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 » Community » Newbie corner » Switching layout inside class
Switching layout inside class [message #48442] Tue, 04 July 2017 11:12 Go to next message
rafiwui is currently offline  rafiwui
Messages: 105
Registered: June 2017
Location: Stuttgart, Germany
Experienced Member
I can't find a possibility to change the layout of a window inside the window class.
So what I want to do is kind of the following:

1. I have two layouts defined in the .lay file
2. I have a class that inherits from TopWindow
3. For this example I add two buttons to my window in the constructor
4. If I press button 1 I want to change the layout to the first layout and if I press button 2 it should show me layout 2.
But I don't want another window to open up!
So I expected it to be something like this:
OnClickButton1()
{
    WithLayout1<TopWindow> layout1;
    SetLayout_Layout1(layout1, true, true);
}

OnClickButton2()
{
    WithLayout2<TopWindow> layout2;
    SetLayout_Layout1(layout1, true, true);
}


Result: the application starts but no layout is shown.
And after inspecting the lay.h file I know why this does not work.
So my second thought then was the following:

OnClickButton1()
{
    WithLayout1<TopWindow> layout1;
    SetLayout_Layout1(*this, layout1, true, true);
}

OnClickButton2()
{
    WithLayout2<TopWindow> layout2;
    SetLayout_Layout1(*this, layout1, true, true);
}


But same thing as before: the layout doesn't show up.

But is there any way to do sth like this?
Is the problem the <TopWindow> that i use as template class when creating the parent object?
Or do I have to write another macro like the ones currently inside the lay.h file?

Probably this macro would look like this: (not tested)
#define LAYOUT(nm, x, y)        template<class T> inline void SetLayoutThis_##nm(T& parent, bool add = false, bool show = false) {
#define UNTYPED(var, param)       parent.var.param; if(add) Add(parent.var); if(show) parent.var.Show();
#define ITEM(class, var, param)   UNTYPED(var, param);
#define END_LAYOUT              }

#include LAYOUTFILE

// undef everything


Greetings
Daniel

[Updated on: Tue, 04 July 2017 11:12]

Report message to a moderator

Re: Switching layout inside class [message #48443 is a reply to message #48442] Tue, 04 July 2017 11:17 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 791
Registered: August 2007
Contributor
Hello,

Traditional U++ way to do what you want to do is to use a ParentCtrl (It is basically Ctrl but used for grouping other ctrls. So the same rules for Ctrl apply to ParentCtrl.):
OnClickButton1()
{
    WithLayout1<ParentCtrl> layout1;
  //...
}

OnClickButton2()
{
    WithLayout2<ParentCtrl> layout2;
  //...
}


Best regards,

Oblivion


[Updated on: Tue, 04 July 2017 11:19]

Report message to a moderator

Re: Switching layout inside class [message #48445 is a reply to message #48443] Tue, 04 July 2017 11:36 Go to previous messageGo to next message
rafiwui is currently offline  rafiwui
Messages: 105
Registered: June 2017
Location: Stuttgart, Germany
Experienced Member
Didn't work with the following code:

#include <CtrlLib/CtrlLib.h>
using namespace Upp;

#define LAYOUTFILE <main/layouts.lay>
#include <CtrlCore/lay.h>

class MyWindow : public TopWindow
{
public:
	Button button1;
	Button button2;
	
	typedef MyWindow CLASSNAME;
	MyWindow();
	
private:
	void OnClickButton1();
	void OnClickButton2();
};

MyWindow::MyWindow()
{
	button1.SetLabel("Button 1").LeftPosZ(10, 100).TopPosZ(20, 30);
	button2.SetLabel("Button 2").RightPosZ(10, 100).TopPosZ(20, 30);
	button1 << THISBACK(OnClickButton1);
	button2 << THISBACK(OnClickButton2);
	Add(button1);
	Add(button2);
}

void MyWindow::OnClickButton1()
{
	Title("Layout 1");
	WithTest1<ParentCtrl> layout1;
	SetLayout_Test1(layout1, true, true);
}

void MyWindow::OnClickButton2()
{
	Title("Layout 2");
	WithTest2<ParentCtrl> layout2;
	SetLayout_Test2(*this, layout2, true, true);
}

GUI_APP_MAIN
{
	MyWindow app;
	app.SetRect(0, 0, Zx(250), Zy(70));
	app.Run();
}


Do I have to do sth with the layout1? Like layout1.Show()? Or Give it a size?
And which of the two SetLayout... is the correct one to use?


Greetings
Daniel

[Updated on: Tue, 04 July 2017 11:37]

Report message to a moderator

Re: Switching layout inside class [message #48451 is a reply to message #48445] Tue, 04 July 2017 13:13 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 791
Registered: August 2007
Contributor
Hello,

Quote:

Do I have to do sth with the layout1? Like layout1.Show()? Or Give it a size?
And which of the two SetLayout... is the correct one to use?



One way of doing it wil be as follows (or something along these lines should work. I can't test it at the moment, I don't have access to TheIDE now:

#include <CtrlLib/CtrlLib.h>
using namespace Upp;

#define LAYOUTFILE <main/layouts.lay>
#include <CtrlCore/lay.h>

class MyWindow : public TopWindow
{
public:
	Button button1;
	Button button2;

        WithTest1<ParentCtrl> layout1; 
        WithTest2<ParentCtrl> layout2;
	
	typedef MyWindow CLASSNAME;
	MyWindow();
	
private:
	void OnClickButton1();
	void OnClickButton2();
};

MyWindow::MyWindow()
{
        CtrlLayout(layout1); 
        CtrlLayout(layout2); 
        layout1.Hide();      
        layout2.Hide();                  
        Add(layout1); // You should set its size or position. E.g. layout1.SizePos() will cover the window's work area.
        Add(layout2); // You should set its size or position. E.g. layout2.SizePos() will cover the window's work area.
       
	button1.SetLabel("Button 1").LeftPosZ(10, 100).TopPosZ(20, 30);
	button2.SetLabel("Button 2").RightPosZ(10, 100).TopPosZ(20, 30);
	button1 << THISBACK(OnClickButton1);
	button2 << THISBACK(OnClickButton2);
	Add(button1);
	Add(button2);

}

void MyWindow::OnClickButton1()
{
	Title("Layout 1");
	layout1.Show();
        layout2.Hide();
}

void MyWindow::OnClickButton2()
{
	Title("Layout 2");
	layout2.Show();
        layout1.Hide();
}

GUI_APP_MAIN
{
	MyWindow app;
	app.SetRect(0, 0, Zx(250), Zy(70));
	app.Run();
}





Best regards,

Oblivion.


[Updated on: Tue, 04 July 2017 13:14]

Report message to a moderator

Re: Switching layout inside class [message #48454 is a reply to message #48451] Tue, 04 July 2017 13:48 Go to previous message
rafiwui is currently offline  rafiwui
Messages: 105
Registered: June 2017
Location: Stuttgart, Germany
Experienced Member
Perfect that worked Smile
Thanks a lot!!


Greetings
Daniel
Previous Topic: cc1plus crash
Next Topic: Adding Tab and Slaves to a TabCtrl
Goto Forum:
  


Current Time: Tue Nov 24 19:04:40 CET 2020

Total time taken to generate the page: 0.00862 seconds