From: Unknown (BV) Date: Wed, 12 May 1999 03:03:26 +0000 (+0000) Subject: Modified OnSashPositionChange to resize the window to minimum pane X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/4c0130922b0ded42c7e4009b64d418fe13b8f987 Modified OnSashPositionChange to resize the window to minimum pane size if the selected position would result in a too-small pane. Edge-detection logic (which closes a split) moved from OnMouseEvent to OnSashPositionChange. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@2422 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/generic/splitter.cpp b/src/generic/splitter.cpp index 526ca5533e..0d926085d4 100644 --- a/src/generic/splitter.cpp +++ b/src/generic/splitter.cpp @@ -140,10 +140,11 @@ void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event)) DrawSash(dc); } + void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event) { - long x, y; - event.Position(&x, &y); + long x = event.GetX(); + long y = event.GetY(); // reset the cursor #ifdef __WXMOTIF__ @@ -186,64 +187,38 @@ void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event) // Erase old tracker DrawSashTracker(m_oldX, m_oldY); + // Obtain window size. We are only interested in the dimension the sash + // splits up int w, h; GetClientSize(&w, &h); - if ( m_splitMode == wxSPLIT_VERTICAL ) + int window_size = (m_splitMode == wxSPLIT_VERTICAL ? w : h ); + int new_sash_position = + (int) ( m_splitMode == wxSPLIT_VERTICAL ? x : y ); + + if ( !OnSashPositionChange(new_sash_position) ) + return; + + if ( new_sash_position == 0 ) + { + // We remove the first window from the view + wxWindow *removedWindow = m_windowOne; + m_windowOne = m_windowTwo; + m_windowTwo = (wxWindow *) NULL; + OnUnsplit(removedWindow); + m_sashPosition = 0; + } + else if ( new_sash_position == window_size ) { - if ( !OnSashPositionChange(x) ) - return; - - if ( x <= 4 ) - { - // We remove the first window from the view - wxWindow *removedWindow = m_windowOne; - m_windowOne = m_windowTwo; - m_windowTwo = (wxWindow *) NULL; - - OnUnsplit(removedWindow); - m_sashPosition = 0; - } - else if ( x >= (w - 4) ) - { - // We remove the second window from the view - wxWindow *removedWindow = m_windowTwo; - m_windowTwo = (wxWindow *) NULL; - OnUnsplit(removedWindow); - m_sashPosition = 0; - } - else - { - m_sashPosition = x; - } + // We remove the second window from the view + wxWindow *removedWindow = m_windowTwo; + m_windowTwo = (wxWindow *) NULL; + OnUnsplit(removedWindow); + m_sashPosition = 0; } - else // m_splitMode == wxSPLIT_VERTICAL + else { - if ( !OnSashPositionChange(y) ) - return; - - if ( y <= 4 ) - { - // We remove the first window from the view - wxWindow *removedWindow = m_windowOne; - m_windowOne = m_windowTwo; - m_windowTwo = (wxWindow *) NULL; - - OnUnsplit(removedWindow); - m_sashPosition = 0; - } - else if ( y >= (h - 4) ) - { - // We remove the second window from the view - wxWindow *removedWindow = m_windowTwo; - m_windowTwo = (wxWindow *) NULL; - OnUnsplit(removedWindow); - m_sashPosition = 0; - } - else - { - m_sashPosition = y; - } - } // m_splitMode == wxSPLIT_VERTICAL + m_sashPosition = new_sash_position; + } SizeWindows(); } // left up && dragging else if (event.Moving() && !event.Dragging()) @@ -669,29 +644,43 @@ void wxSplitterWindow::SetSashPosition(int position, bool redraw) } } -bool wxSplitterWindow::OnSashPositionChange(int newSashPosition) +bool wxSplitterWindow::OnSashPositionChange(int& newSashPosition) { - // is the left/upper pane too small? - if ( newSashPosition < m_minimumPaneSize ) - return NULL; - - // is the right/lower pane too small? - int w, h; - GetClientSize(&w, &h); - - if ( m_splitMode == wxSPLIT_VERTICAL ) - { - if ( w - newSashPosition < m_minimumPaneSize ) - return FALSE; - } - else // m_splitMode = wxSPLIT_HORIZONTAL - { - if ( h - newSashPosition < m_minimumPaneSize ) - return FALSE; - } - - // it's ok to move sash - return TRUE; + // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure. + const int UNSPLIT_THRESHOLD = 4; + + if (newSashPosition <= UNSPLIT_THRESHOLD) // threshold top / left check + { + newSashPosition = 0; + return TRUE; + } + + // Obtain relevant window dimension for bottom / right threshold check + int w, h; + GetClientSize(&w, &h); + int window_size = (m_splitMode == wxSPLIT_VERTICAL) ? w : h ; + + if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD ) + { + newSashPosition = window_size; + return TRUE; + } + + // If resultant pane would be too small, enlarge it. + + // Check upper / left pane + if ( newSashPosition < m_minimumPaneSize ) + newSashPosition = m_minimumPaneSize; // NB: don't return just yet + + // Check lower / right pane (check even if sash was just adjusted) + if ( newSashPosition > window_size - m_minimumPaneSize ) + newSashPosition = window_size - m_minimumPaneSize; + + // If the result is out of bounds it means minimum size is too big, so + // split window in half as best compromise. + if (newSashPosition < 0 || newSashPosition > window_size) + newSashPosition = window_size / 2; + return TRUE; } // Called when the sash is double-clicked.