Re: Managing modeless document windows

Kenneth Porter <[email protected]>
Newsgroups gmane.comp.lib.wxwindows.general
Message-ID <2680E8FE38075C13ABB4BB5F@[10.96.7.39]>
--On Tuesday, April 04, 2023 2:58 AM +0200 Vadim Zeitlin 
<[email protected]> wrote:

>  I would hold the global container (probably a map indexed by the document
> identity, whatever it is) in YourApp, i.e. wxApp-derived class and remove
> the document frames from it when they're closed/destroyed, i.e. in their
> own wxEVT_CLOSE_WINDOW handler/dtor.

I went with this. The main frame (or the app) holds the instance and 
anything that creates an independent frame adds it to this container. Each 
frame's OnClose handler invokes the container's destroy method.

I'm using it in a database browser to open frames displaying the contents 
of database tables.

class ModelessWindowManager
{
   std::vector<wxWindow*> windows; // to be deleted on destruction
public:
   ~ModelessWindowManager();
   void add(wxWindow* window);
   void destroy(wxWindow* window);
};

void ModelessWindowManager::add(wxWindow* window)
{
   windows.push_back(window);
}

// invoked from owned windows' OnClose event

void ModelessWindowManager::destroy(wxWindow* window)
{
   // destroy the window and remove it from our list
   window->Destroy();
   // https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
   windows.erase(std::remove(windows.begin(), windows.end(), window), 
windows.end());
}

ModelessWindowManager::~ModelessWindowManager()
{
   while (!windows.empty())
   {
      windows.back()->Destroy();
      windows.pop_back();
   }
}


-- 
Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
--- 
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/2680E8FE38075C13ABB4BB5F%40%5B10.96.7.39%5D.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.