diff --git a/include/wx/window.h b/include/wx/window.h index 92072f9b52..598d547a3f 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -784,6 +784,10 @@ public: // needed just for extended runtime const wxWindowList& GetWindowChildren() const { return GetChildren() ; } + // call the specified functor for all children, recursively + template + void CallForEachChild(const T& fn); + // get the window before/after this one in the parents children list, // returns nullptr if this is the first/last window wxWindow *GetPrevSibling() const { return DoGetSibling(OrderBefore); } @@ -2057,6 +2061,15 @@ inline wxWindow *wxWindowBase::GetGrandParent() const return m_parent ? m_parent->GetParent() : nullptr; } +template +void wxWindowBase::CallForEachChild(const T& fn) +{ + fn(static_cast(this)); + + for ( auto& child : GetChildren() ) + child->CallForEachChild(fn); +} + // ---------------------------------------------------------------------------- // global functions // ---------------------------------------------------------------------------- diff --git a/interface/wx/window.h b/interface/wx/window.h index 28d54b6fec..e5f2489052 100644 --- a/interface/wx/window.h +++ b/interface/wx/window.h @@ -592,6 +592,30 @@ public: */ virtual void AddChild(wxWindow* child); + /** + Invoke the given functor for all children of the given window + recursively. + + This function calls @a functor for the window itself and then for all + of its children, recursively. + + Example of using it for implementing a not very smart translation + function: + @code + void MyFrame::OnTranslate(wxCommandEvent&) { + CallForEachChild([](wxWindow* win) { + wxString rest; + if ( win->GetLabel().StartsWith("Hello ", &rest) ) + win->SetLabel("Bonjour " + rest); + }); + } + @endcode + + @since 3.3.0 + */ + template + void CallForEachChild(const T& functor); + /** Destroys all children of a window. Called automatically by the destructor. */