How to disable subwindows when main window is disabled?

Volker Bartheld dr_versaeg at freenet.de
Tue Dec 11 09:52:37 PST 2007


Hi!

>> you can use
>> wxWindow::GetChildren() to access it and grey out everything recursively.

Just to share my efforts with the community - here's some code:

  typedef std::map<wxWindowID, bool> WindowState_t;
  WindowState_t m_WindowState;
  bool MyDialog::m_areChildrenEnabled=true;

  static void MyDialog::EnumWindows(wxWindow* pWin, WindowState_t& state)
  {
    wxWindowList WindowList=pWin->GetChildren();
    for(wxWindowList::iterator it=WindowList.begin(); WindowList.end()!=it; ++it)
    {
      state[(*it)->GetId()]=(*it)->IsEnabled();
      EnumWindows(*it, state);
    }
  }
  virtual bool MyDialog::Enable(bool enable=true)
  {
    if(m_areChildrenEnabled==enable) return enable; // nothing to do, children already have the required state
    m_areChildrenEnabled=enable;
    if(!enable) { m_WindowState.clear(); EnumWindows(this, m_WindowState); }
    for(WindowState_t::const_iterator cit=m_WindowState.begin(); m_WindowState.end()!=cit; ++cit)
    {
      wxWindow* pWin=FindWindow(cit->first);
      if(pWin) pWin->Enable(enable?cit->second:false);
    } // for(WindowState_t::const_iterator cit=state.begin(); state.end()!=cit; ++cit)
    Refresh();
    return enable;
  }

Note, that I'm _not_ disabling the main window. This will defunct also the minimize
and other system menus and inhibit moving (at least with wxMSW). MyDialog::Enable()
remembers the previous states in a std::map<> which makes it easy to return to
how everything was before without too much hassle.

HTH & have a nice evening,
Volker


-- 
mailto:  V B A R T H E L D at G M X dot D E






More information about the wx-users mailing list