How to gracefully close all dialogs on Application exit

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
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

How to gracefully close all dialogs on Application exit

Post by deepti »

Hi All,

I have a wxDialog instance which on click of a button, creates another instance of wxDialog, and this in turn creates another wxDialog on click of a button. It is basically a nested parent-child relationship.
Each wxDialog instance is a member variable of its parent class.
On application exit, should calling the Destroy() on the top-most parent wxDialog instance suffice? Will this take care of recursively calling Destroy() on all its child and grand-child dialogs?

Please let me know.
Last edited by deepti on Mon Sep 17, 2018 5:38 am, edited 1 time in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to gracefully close all dialogs on Application exit

Post by ONEEYEMAN »

Hi,
How do you create those dialogs? Can you show some code?

Thank you.
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: How to gracefully close all dialogs on Application exit

Post by deepti »

Hi,
Please find below the code explaining the creation of the dialogs.

So, there is a dialog "WxMainDialog" which has a hyperlink. On click of that, another dialog "WxAdvancedSettingsDialog" will be launched, as shown in the code below:
"m_asd" is member variable of class WxMainDialog.

Code: Select all

void WxMainDialog::OnSelectiveLink(wxHyperlinkEvent& WXUNUSED(event))
{
	m_asd = new WxAdvancedSettingsDialog(2, NULL);
	m_asd->SetFocus();
	m_asd->Raise();
	if (m_asd->ShowModal() == wxID_OK)
	{
	}
	m_asd->Destroy();
	m_asd = 0;
}

The "WxAdvancedSettingsDialog" dialog has an "Edit Schedule" button. On click of that, there is another "WxScheduleDialog" dialog which will be launched, as seen below:
"m_scheduleDlg" is member variable of class "WxAdvancedSettingsDialog".

Code: Select all

void WxAdvancedSettingsDialog::OnEditActiveSyncSchedule(wxCommandEvent& event)
{
	m_scheduleDlg = new WxScheduleDialog(TIME_ACTIVE_CONTROL);

	
	m_scheduleDlg->SetFocus();
	m_scheduleDlg->Raise();
	m_scheduleDlg->Show(true);

	.....
	.....
	if (m_scheduleDlg->ShowModal() == wxID_OK)
	{
		//do something	
	}
	m_scheduleDlg->Destroy();
}
The problem is when the last dialog (WxScheduleDialog), or even the second one "WxAdvancedSettingsDialog" is kept open, and the application is exited, the app does not exit properly. It is still lingering as can be seen in the Task Manager.
So, I need a way to destroy all these dialogs from the "OnExit" function of the app, where the uppermost dialog "WxMainDialog" is destroyed.
In the destructor of "WxMainDialog", I am calling the Destroy() on "WxAdvancedSettingsDialog" instance.
And in the destructor of "WxAdvancedSettingsDialog", i am calling the Destroy() on "WxScheduleDialog" instance.
Would this suffice?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to gracefully close all dialogs on Application exit

Post by ONEEYEMAN »

Hi,
Which window is set to be the parent for the wxAdvancedeSettings Dialog and wxScheduleDialog?
Do they have a NULL parent or some window in your program hierarchy?

Thank you.
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: How to gracefully close all dialogs on Application exit

Post by deepti »

Yeah, they have NULL set as parent. Should that be changed to the dialog which is creating them?
Thank you!
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to gracefully close all dialogs on Application exit

Post by doublemax »

You could try wxTheApp->ExitMainLoop(). But i don't know if it works with open modal dialogs.
Use the source, Luke!
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: How to gracefully close all dialogs on Application exit

Post by deepti »

Hi,

The wxTheApp->ExitMainLoop() call didn't help :(
Should i set the parent of the dialog to the one that created it?
With this, it seems to be solving the issue, but it looks kinda weird, because the main dialog of the app gets closed before the child dialogs. But the app is exiting. But, is this a clean approach?

Please let me know..
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to gracefully close all dialogs on Application exit

Post by ONEEYEMAN »

Yes, it is a good approach.

Thank you.
art-ganseforth
Earned some good credits
Earned some good credits
Posts: 147
Joined: Mon Sep 01, 2014 10:14 am

Re: How to gracefully close all dialogs on Application exit

Post by art-ganseforth »

Maybe you use sonething like this:

Code: Select all

void myDialog::OnCloseParent (wxEvent &ev) {
    ev.Skip();
    Close(); // maybe use Destoy() here
}

myDialog::myDialog (myDialog parent, ...) : wxDialog (...)  {
    parent->Connect(wxEVT_CLOSE, wxEvtHandler(myDialog::OnCloseParent ), NULL, this);

    ...

I wrote this out of my head. So something might be wrong, but i think the concept may work...

Best,
Frank
Post Reply