diff --git a/docs/changes.txt b/docs/changes.txt index f1f3b42405..c2463065dd 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -127,6 +127,7 @@ All (GUI): - Fixed crash in wxHtmlHelpController if the help window is still open. - Fixed generic art provider to scale bitmaps down to client-specific best size if needed. +- Made wxSpinCtrl::Reparent() in MSW and generic versions (Angelo Mottola) All (Unix): diff --git a/include/wx/generic/spinctlg.h b/include/wx/generic/spinctlg.h index 50e6ef6afe..3d6377307f 100644 --- a/include/wx/generic/spinctlg.h +++ b/include/wx/generic/spinctlg.h @@ -74,6 +74,7 @@ public: // forward these functions to all subcontrols virtual bool Enable(bool enable = true); virtual bool Show(bool show = true); + virtual bool Reparent(wxWindow *newParent); // get the subcontrols wxTextCtrl *GetText() const { return m_text; } diff --git a/include/wx/msw/spinctrl.h b/include/wx/msw/spinctrl.h index 2a5f56f5c0..bfbf21ce1e 100644 --- a/include/wx/msw/spinctrl.h +++ b/include/wx/msw/spinctrl.h @@ -73,6 +73,8 @@ public: virtual bool Enable(bool enable = true); virtual bool Show(bool show = true); + virtual bool Reparent(wxWindowBase *newParent); + // wxSpinButton doesn't accept focus, but we do virtual bool AcceptsFocus() const { return wxWindow::AcceptsFocus(); } diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index 6e70acb1f7..54aa927364 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -265,6 +265,17 @@ bool wxSpinCtrl::Show(bool show) return true; } +bool wxSpinCtrl::Reparent(wxWindow *newParent) +{ + if ( m_btn ) + { + m_btn->Reparent(newParent); + m_text->Reparent(newParent); + } + + return true; +} + // ---------------------------------------------------------------------------- // value and range access // ---------------------------------------------------------------------------- diff --git a/src/msw/spinctrl.cpp b/src/msw/spinctrl.cpp index 0b22a278e6..ce21c8aecf 100644 --- a/src/msw/spinctrl.cpp +++ b/src/msw/spinctrl.cpp @@ -530,6 +530,46 @@ bool wxSpinCtrl::Show(bool show) return true; } +bool wxSpinCtrl::Reparent(wxWindowBase *newParent) +{ + // Reparenting both the updown control and its buddy does not seem to work: + // they continue to be connected somehow, but visually there is no feedback + // on the buddy edit control. To avoid this problem, we reparent the buddy + // window normally, but we recreate the updown control and reassign its + // buddy. + + if ( !wxWindowBase::Reparent(newParent) ) + return false; + + newParent->GetChildren().DeleteObject(this); + + // preserve the old values + const wxSize size = GetSize(); + int value = GetValue(); + const wxRect btnRect = wxRectFromRECT(wxGetWindowRect(GetHwnd())); + + // destroy the old spin button + UnsubclassWin(); + if ( !::DestroyWindow(GetHwnd()) ) + wxLogLastError(wxT("DestroyWindow")); + + // create and initialize the new one + if ( !wxSpinButton::Create(GetParent(), GetId(), + btnRect.GetPosition(), btnRect.GetSize(), + GetWindowStyle(), GetName()) ) + return false; + + SetValue(value); + SetRange(m_min, m_max); + SetInitialSize(size); + + // associate it with the buddy control again + ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent())); + (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0); + + return true; +} + bool wxSpinCtrl::Enable(bool enable) { if ( !wxControl::Enable(enable) )