diff --git a/include/wx/generic/splitter.h b/include/wx/generic/splitter.h index fabcbfcf8a..3d1745b0a1 100644 --- a/include/wx/generic/splitter.h +++ b/include/wx/generic/splitter.h @@ -237,8 +237,13 @@ protected: // get either width or height depending on the split mode int GetWindowSize() const; - - // set m_sashPosition w/ safeguards + + // convert the user specified sash position which may be > 0 (as is), < 0 + // (specifying the size of the right pane) or 0 (use default) to the real + // position to be passed to DoSetSashPosition() + int ConvertSashPosition(int sashPos) const; + + // set the real sash position, sashPos here must be positive void DoSetSashPosition(int sashPos); wxSplitMode m_splitMode; diff --git a/samples/splitter/splitter.cpp b/samples/splitter/splitter.cpp index 60fa68fe6e..800421eab9 100644 --- a/samples/splitter/splitter.cpp +++ b/samples/splitter/splitter.cpp @@ -186,6 +186,7 @@ MyFrame::MyFrame() m_right->Show(FALSE); m_splitter->Initialize(m_left); #else + // you can also try -100 m_splitter->SplitVertically(m_left, m_right, 100); #endif @@ -210,6 +211,8 @@ void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) ) m_left->Show(TRUE); m_right->Show(TRUE); m_splitter->SplitHorizontally( m_left, m_right ); + + SetStatusText(_T("Splitter split horizontally"), 1); } void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) ) @@ -219,6 +222,8 @@ void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) ) m_left->Show(TRUE); m_right->Show(TRUE); m_splitter->SplitVertically( m_left, m_right ); + + SetStatusText(_T("Splitter split vertically"), 1); } void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) ) diff --git a/src/generic/splitter.cpp b/src/generic/splitter.cpp index 0ee00d2fd3..448d795d35 100644 --- a/src/generic/splitter.cpp +++ b/src/generic/splitter.cpp @@ -718,13 +718,17 @@ int wxSplitterWindow::AdjustSashPosition(int sashPos) const void wxSplitterWindow::DoSetSashPosition(int sashPos) { - m_requestedSashPosition = sashPos; - m_sashPosition = sashPos == 0 ? 0 : AdjustSashPosition(sashPos); + int newSashPosition = AdjustSashPosition(sashPos); - wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this); - event.m_data.pos = m_sashPosition; + if ( newSashPosition != m_sashPosition ) + { + m_sashPosition = newSashPosition; - (void)DoSendEvent(event); + wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this); + event.m_data.pos = m_sashPosition; + + (void)DoSendEvent(event); + } } // Position and size subwindows. @@ -732,8 +736,21 @@ void wxSplitterWindow::DoSetSashPosition(int sashPos) // including the edges next to the sash. void wxSplitterWindow::SizeWindows() { - if ( m_requestedSashPosition != m_sashPosition ) - DoSetSashPosition(m_requestedSashPosition); + // check if we have delayed setting the real sash position + if ( m_requestedSashPosition != INT_MAX ) + { + int newSashPosition = ConvertSashPosition(m_requestedSashPosition); + if ( newSashPosition != m_sashPosition ) + { + DoSetSashPosition(newSashPosition); + } + + if ( newSashPosition == m_sashPosition ) + { + // don't update it any more + m_requestedSashPosition = INT_MAX; + } + } int w, h; GetClientSize(&w, &h); @@ -794,32 +811,39 @@ bool wxSplitterWindow::DoSplit(wxSplitMode mode, if ( IsSplit() ) return FALSE; - int window_size = GetWindowSize(); - m_splitMode = mode; m_windowOne = window1; m_windowTwo = window2; - if ( sashPosition > 0 ) - { - DoSetSashPosition(sashPosition); - } - else if ( sashPosition < 0 ) - { - // It's negative so adding is subtracting - DoSetSashPosition(window_size + sashPosition); - } - else - { - // default - DoSetSashPosition(window_size/2); - } + // remember the sash position we want to set for later if we can't set it + // right now (e.g. because the window is too small) + m_requestedSashPosition = sashPosition; + + DoSetSashPosition(ConvertSashPosition(sashPosition)); SizeWindows(); return TRUE; } +int wxSplitterWindow::ConvertSashPosition(int sashPosition) const +{ + if ( sashPosition > 0 ) + { + return sashPosition; + } + else if ( sashPosition < 0 ) + { + // It's negative so adding is subtracting + return GetWindowSize() + sashPosition; + } + else // sashPosition == 0 + { + // default, put it in the centre + return GetWindowSize() / 2; + } +} + // Remove the specified (or second) window from the view // Doesn't actually delete the window. bool wxSplitterWindow::Unsplit(wxWindow *toRemove)