Adding window on layout from another window Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MaxWell2021
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Mar 06, 2022 10:16 am
Location: Durban, KwaZulu-Natal, South Africa

Adding window on layout from another window

Post by MaxWell2021 »

I want to add a control/window from another window when it is created by the user. I created a custom control that a user will create by pressing a button. The control should then appear on two different windows.

So I decided to Create a class defining a new panel that has wxGridSizer as it layout sizer.

Code: Select all

class SubjectPanel : public wxPanel
{
	DECLARE_ABSTRACT_CLASS(SubjectPanel)
public:
	SubjectPanel(wxWindow* parent, wxWindowID winid, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
		long style = wxBORDER_NONE);

	void SetLayout();

	void AddSubject(wxControl* subject);

protected:
	wxGridSizer* sizer;
};
The implementaion is as follows:

Code: Select all

void SubjectPanel::SetLayout()
{
	// create sizer to create open space around the subject objects
	wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);

	// Align the subject objects in a Grid format
	sizer = new wxGridSizer(0, 4, 8, 8);
	topsizer->Add(sizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);

	// set the sizer as layout controller
	SetSizer(topsizer);
	topsizer->Fit(this);
	topsizer->SetSizeHints(this);
}

void SubjectPanel::AddSubject(wxControl* subject)
{
	// Add subject control on page 
	sizer->Add(subject, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
}
Then the control that will be created as follows on a button event:

Code: Select all

void wxAddSubjectPanel::OnGradeOneSubject(wxCommandEvent& event)
{
	// Find users profile account 
	wxSchoolPage* schoolProfile = (wxSchoolPage*)FindWindow(ID_SCHOOL_PROFILE);

	wxString subsimage = subimage->GetImage();
	wxString subjectname = subnameCtrl->GetValue();
	wxString assignedTeacher = teachercombo->GetValue();

	//subject panel
	SubjectPanel* pagesforgradeone = (SubjectPanel*)FindWindow(ID_GRADE_ONE_PAGE);

	//Creating subject page.
	SubObject* subject = new SubObject(this, wxID_ANY, subjectname, assignedTeacher, subsimage, wxDefaultPosition,
		wxDefaultSize);
	schoolProfile->sub.Add(subject);

	pagesforgradeone->AddSubject(subject);
}
but when I am debugging it returns an error with an exeption

Code: Select all

bool wxSizer::AreAnyItemsShown() const
{
    wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
    while (node)
    {
        if ( node->GetData()->IsShown() )
            return true;
        node = node->GetNext();
    }

    return false;
}
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: Adding window on layout from another window

Post by doublemax@work »

That doesn't work. A window has one parent and you can't display it more than once with different parents.
MaxWell2021
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Mar 06, 2022 10:16 am
Location: Durban, KwaZulu-Natal, South Africa

Re: Adding window on layout from another window

Post by MaxWell2021 »

doublemax@work wrote: Mon May 23, 2022 10:36 am That doesn't work. A window has one parent and you can't display it more than once with different parents.
What if it create a default constructor of the window subjectobject() and not allocate a parent for the window?
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: Adding window on layout from another window

Post by doublemax@work »

MaxWell2021 wrote: Mon May 23, 2022 1:00 pm
doublemax@work wrote: Mon May 23, 2022 10:36 am That doesn't work. A window has one parent and you can't display it more than once with different parents.
What if it create a default constructor of the window subjectobject() and not allocate a parent for the window?
No. If you want a window to appear multiple times, you need a separate instance for each one.
MaxWell2021
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Mar 06, 2022 10:16 am
Location: Durban, KwaZulu-Natal, South Africa

Re: Adding window on layout from another window

Post by MaxWell2021 »

doublemax@work wrote: Mon May 23, 2022 1:41 pm
MaxWell2021 wrote: Mon May 23, 2022 1:00 pm
doublemax@work wrote: Mon May 23, 2022 10:36 am That doesn't work. A window has one parent and you can't display it more than once with different parents.
What if it create a default constructor of the window subjectobject() and not allocate a parent for the window?
No. If you want a window to appear multiple times, you need a separate instance for each one.
Then can you please tell me another way I can do it I wanted to create a metafile control that can be saved as a form of data and access through out the application. For example Youtube,... users post videos and these videos then appear on the home page (Just making an example). So I can go about this?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Adding window on layout from another window

Post by ONEEYEMAN »

Hi,
Every time you create a window, you need a new instance of every control you want to put on it.

Code: Select all

Window1::Window1()
{
      myControl1 = new MyControl();
}

Window2::Window2()
{
    myControl2 = new MyControl();
}
You can't use myControl1 inside Window2 - you have to have a new instance.

There is no other way.

Going to your analogy - you open the browser, go to youtube and create a video (myControl1).
But when you push this video on you home page - every time you access it your browser will create a new instance of youtube control (myControl2).
It will not reuse the one you used when you created the window.

Thank you.
MaxWell2021
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Mar 06, 2022 10:16 am
Location: Durban, KwaZulu-Natal, South Africa

Re: Adding window on layout from another window

Post by MaxWell2021 »

ONEEYEMAN wrote: Mon May 23, 2022 3:38 pm Hi,
Every time you create a window, you need a new instance of every control you want to put on it.

Code: Select all

Window1::Window1()
{
      myControl1 = new MyControl();
}

Window2::Window2()
{
    myControl2 = new MyControl();
}
You can't use myControl1 inside Window2 - you have to have a new instance.

There is no other way.

Going to your analogy - you open the browser, go to youtube and create a video (myControl1).
But when you push this video on you home page - every time you access it your browser will create a new instance of youtube control (myControl2).
It will not reuse the one you used when you created the window.

Thank you.
I'm not sue if I really understand can you make another example to make it more clear I was think of creating a copy constructor as my next solution
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Adding window on layout from another window

Post by doublemax »

Can you give an concrete example of what you want to do?

In general, this seems to be a classical model-view-(controller?) problem. You have one set of data (model) which you want to display in multiple places (the views). And whenever the mode changes, you need to update all views.

Read up on "MVC pattern".
Use the source, Luke!
MaxWell2021
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Mar 06, 2022 10:16 am
Location: Durban, KwaZulu-Natal, South Africa

Re: Adding window on layout from another window

Post by MaxWell2021 »

doublemax wrote: Mon May 23, 2022 4:14 pm Can you give an concrete example of what you want to do?

In general, this seems to be a classical model-view-(controller?) problem. You have one set of data (model) which you want to display in multiple places (the views). And whenever the mode changes, you need to update all views.

Read up on "MVC pattern".
Isn't MVC for web applications?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Adding window on layout from another window

Post by ONEEYEMAN »

Hi,
No.
It is very fundamental design pattern.
Web application have something different.

Google "model-view-controller" for more info...

Thank you.
MaxWell2021
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Mar 06, 2022 10:16 am
Location: Durban, KwaZulu-Natal, South Africa

Re: Adding window on layout from another window

Post by MaxWell2021 »

ONEEYEMAN wrote: Mon May 23, 2022 10:32 pm Hi,
No.
It is very fundamental design pattern.
Web application have something different.

Google "model-view-controller" for more info...

Thank you.
I have read through the MVC design pattern and it exactly what I needed, Thanks in Advance. I wanted to ask if it is a good idea to create a copy constructor for the control. and then creating a copy of the object that have been created by users in another window. Process is as follows:
User creates the object on a window and the view creates a copy of the control to it using the copy constructor.

Is this good?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Adding window on layout from another window

Post by ONEEYEMAN »

Hi,
Yes, it is good.
You unfortunately didn't articulate it properly. ;-)

Thank you.
Post Reply