]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/splitter.cpp
Fixed toolbar crash for MinGW/Cygwin
[wxWidgets.git] / src / generic / splitter.cpp
index 4aad72698ca22042235178ff6bae494010ea1d96..1d29a0b782c741db3e6660a10d84dee60b85af19 100644 (file)
@@ -33,6 +33,7 @@
 #include "wx/dcscreen.h"
 #include "wx/settings.h"
 #include "wx/log.h"
+#include "wx/utils.h"
 
 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED)
 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING)
@@ -88,6 +89,13 @@ bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id,
     else
         m_borderSize = 0;
 
+#ifdef __WXMAC__
+    int major,minor;
+    wxGetOsVersion( &major, &minor );
+    if (major >= 10)
+        m_windowStyle |= wxSP_SASH_AQUA;
+#endif
+
     return TRUE;
 }
 
@@ -106,7 +114,7 @@ void wxSplitterWindow::Init()
     m_firstY = 0;
     m_sashSize = 7;
     m_borderSize = 2;
-    m_sashPosition = 0;
+    m_sashPosition = m_requestedSashPosition = 0;
     m_minimumPaneSize = 0;
     m_sashCursorWE = new wxCursor(wxCURSOR_SIZEWE);
     m_sashCursorNS = new wxCursor(wxCURSOR_SIZENS);
@@ -158,8 +166,8 @@ void wxSplitterWindow::OnIdle(wxIdleEvent& event)
 
 void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
 {
-    wxCoord x = (wxCoord)event.GetX(),
-            y = (wxCoord)event.GetY();
+    int x = (int)event.GetX(),
+        y = (int)event.GetY();
 
     // reset the cursor
 #ifdef __WXMOTIF__
@@ -213,11 +221,8 @@ void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
 
         // Obtain window size. We are only interested in the dimension the sash
         // splits up
-        int w, h;
-        GetClientSize(&w, &h);
-        int window_size = (m_splitMode == wxSPLIT_VERTICAL ? w : h );
-        int new_sash_position =
-            (int) ( m_splitMode == wxSPLIT_VERTICAL ? x : y );
+        int window_size = GetWindowSize();
+        int new_sash_position = m_splitMode == wxSPLIT_VERTICAL ? x : y;
 
         wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED,
                                       this);
@@ -242,7 +247,7 @@ void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
                 m_windowOne = m_windowTwo;
                 m_windowTwo = (wxWindow *) NULL;
                 SendUnsplitEvent(removedWindow);
-                m_sashPosition = 0;
+                DoSetSashPosition(0);
             }
             else if ( new_sash_position == window_size )
             {
@@ -250,16 +255,16 @@ void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
                 wxWindow *removedWindow = m_windowTwo;
                 m_windowTwo = (wxWindow *) NULL;
                 SendUnsplitEvent(removedWindow);
-                m_sashPosition = 0;
+                DoSetSashPosition(0);
             }
             else
             {
-                m_sashPosition = new_sash_position;
+                DoSetSashPosition(new_sash_position);
             }
         }
         else
         {
-            m_sashPosition = new_sash_position;
+            DoSetSashPosition(new_sash_position);
         }
 
         SizeWindows();
@@ -308,8 +313,7 @@ void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
 
         // Obtain window size. We are only interested in the dimension the sash
         // splits up
-        int new_sash_position =
-            (int) ( m_splitMode == wxSPLIT_VERTICAL ? x : y );
+        int new_sash_position = m_splitMode == wxSPLIT_VERTICAL ? x : y;
 
         wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING,
                                       this);
@@ -362,7 +366,7 @@ void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
         }
         else
         {
-            m_sashPosition = new_sash_position;
+            DoSetSashPosition(new_sash_position);
             m_needUpdating = TRUE;
         }
     }
@@ -389,42 +393,50 @@ void wxSplitterWindow::OnSize(wxSizeEvent& event)
     }
 
     bool iconized = FALSE;
-    wxFrame *frame = wxDynamicCast(parent, wxFrame);
-    if ( frame )
-        iconized = frame->IsIconized();
-    else
+
+    // wxMotif doesn't yet have a wxTopLevelWindow implementation
+#ifdef __WXMOTIF__
+    wxFrame *winTop = wxDynamicCast(parent, wxFrame);
+#else
+    wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
+#endif
+    if ( winTop )
     {
-        wxDialog *dialog = wxDynamicCast(parent, wxDialog);
-        if ( dialog )
-            iconized = dialog->IsIconized();
-        else
-            wxFAIL_MSG(wxT("should have a top level frame or dialog parent!"));
+        iconized = winTop->IsIconized();
     }
+#ifndef __WXMOTIF__
+    else
+    {
+        wxFAIL_MSG(wxT("should have a top level parent!"));
 
+        iconized = FALSE;
+    }
+#endif
+    
     if ( iconized )
     {
         event.Skip();
+
+        return;
     }
-    else
+
+    int cw, ch;
+    GetClientSize( &cw, &ch );
+    if ( m_windowTwo )
     {
-        int cw, ch;
-        GetClientSize( &cw, &ch );
-        if ( m_windowTwo )
+        if ( m_splitMode == wxSPLIT_VERTICAL )
         {
-            if ( m_splitMode == wxSPLIT_VERTICAL )
-            {
-                if ( m_sashPosition >= (cw - 5) )
-                    m_sashPosition = wxMax(10, cw - 40);
-            }
-            if ( m_splitMode == wxSPLIT_HORIZONTAL )
-            {
-                if ( m_sashPosition >= (ch - 5) )
-                    m_sashPosition = wxMax(10, ch - 40);
-            }
+            if ( m_sashPosition >= (cw - 5) )
+                DoSetSashPosition(wxMax(10, cw - 40));
+        }
+        else // m_splitMode == wxSPLIT_HORIZONTAL
+        {
+            if ( m_sashPosition >= (ch - 5) )
+                DoSetSashPosition(wxMax(10, ch - 40));
         }
-
-        SizeWindows();
     }
+
+    SizeWindows();
 }
 
 bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
@@ -535,7 +547,7 @@ void wxSplitterWindow::DrawSash(wxDC& dc)
             else
                 dc.SetPen(*m_darkShadowPen);
             dc.DrawLine(m_sashPosition+m_sashSize-1, m_borderSize, m_sashPosition+m_sashSize-1, h-m_borderSize );
-            
+
             // Draw the top and bottom edges of the sash, if requested
             if (GetWindowStyle() & wxSP_FULLSASH)
             {
@@ -681,18 +693,74 @@ void wxSplitterWindow::DrawSashTracker(int x, int y)
     screenDC.SetBrush(wxNullBrush);
 }
 
+int wxSplitterWindow::GetWindowSize() const
+{
+    wxSize size = GetClientSize();
+
+    return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y;
+}
+
+int wxSplitterWindow::AdjustSashPosition(int sashPos) const
+{
+    int window_size = GetWindowSize();
+
+    wxWindow *win;
+
+    win = GetWindow1();
+    if ( win )
+    {
+        // the window shouldn't be smaller than its own minimal size nor
+        // smaller than the minimual pane size specified for this splitter
+        int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
+                                                      : win->GetMinHeight();
+
+        if ( minSize == -1 || m_minimumPaneSize > minSize )
+            minSize = m_minimumPaneSize;
+
+        minSize += GetBorderSize();
+
+        if ( sashPos < minSize )
+            sashPos = minSize;
+    }
+
+    win = GetWindow2();
+    if ( win )
+    {
+        int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
+                                                      : win->GetMinHeight();
+
+        if ( minSize == -1 || m_minimumPaneSize > minSize )
+            minSize = m_minimumPaneSize;
+
+        int maxSize = window_size - minSize - GetBorderSize();
+        if ( sashPos > maxSize )
+            sashPos = maxSize;
+    }
+
+    return sashPos;
+}
+
+void wxSplitterWindow::DoSetSashPosition(int sashPos)
+{
+    m_requestedSashPosition = sashPos;
+    m_sashPosition = (sashPos == 0) ? 0 : AdjustSashPosition(sashPos);
+}
+
 // Position and size subwindows.
 // Note that the border size applies to each subwindow, not
 // including the edges next to the sash.
 void wxSplitterWindow::SizeWindows()
 {
+    if ( m_requestedSashPosition != m_sashPosition )
+        DoSetSashPosition(m_requestedSashPosition);
+
     int w, h;
     GetClientSize(&w, &h);
 
     if ( GetWindow1() && !GetWindow2() )
     {
-        GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(), w - 2*GetBorderSize(), h - 2*GetBorderSize());
-
+        GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(), 
+                              w - 2*GetBorderSize(), h - 2*GetBorderSize());
     }
     else if ( GetWindow1() && GetWindow2() )
     {
@@ -732,59 +800,45 @@ void wxSplitterWindow::Initialize(wxWindow *window)
 {
     m_windowOne = window;
     m_windowTwo = (wxWindow *) NULL;
-    m_sashPosition = 0;
+    DoSetSashPosition(0);
 }
 
 // Associates the given window with window 2, drawing the appropriate sash
 // and changing the split mode.
 // Does nothing and returns FALSE if the window is already split.
-bool wxSplitterWindow::SplitVertically(wxWindow *window1, wxWindow *window2, int sashPosition)
+bool wxSplitterWindow::DoSplit(wxSplitMode mode,
+                               wxWindow *window1, wxWindow *window2,
+                               int sashPosition)
 {
     if ( IsSplit() )
         return FALSE;
 
-    int w, h;
-    GetClientSize(&w, &h);
+    int window_size = GetWindowSize();
 
-    m_splitMode = wxSPLIT_VERTICAL;
+    m_splitMode = mode;
     m_windowOne = window1;
     m_windowTwo = window2;
-    if ( sashPosition > 0 )
-        m_sashPosition = sashPosition;
-    else if ( sashPosition < 0 )
-        m_sashPosition = w + sashPosition;   // It's negative so adding is subtracting
-    else    // default
-        m_sashPosition = w/2;
 
-    SizeWindows();
-
-    return TRUE;
-}
-
-bool wxSplitterWindow::SplitHorizontally(wxWindow *window1, wxWindow *window2, int sashPosition)
-{
-    if ( IsSplit() )
-        return FALSE;
-
-    int w, h;
-    GetClientSize(&w, &h);
-
-    m_splitMode = wxSPLIT_HORIZONTAL;
-    m_windowOne = window1;
-    m_windowTwo = window2;
     if ( sashPosition > 0 )
-        m_sashPosition = sashPosition;
+    {
+        DoSetSashPosition(sashPosition);
+    }
     else if ( sashPosition < 0 )
-        m_sashPosition = h + sashPosition; // It's negative so adding is subtracting
-    else    // default
-        m_sashPosition = h/2;
+    {
+        // It's negative so adding is subtracting
+        DoSetSashPosition(window_size + sashPosition);
+    }
+    else
+    {
+        // default
+        DoSetSashPosition(window_size/2);
+    }
 
     SizeWindows();
 
     return TRUE;
 }
 
-
 // Remove the specified (or second) window from the view
 // Doesn't actually delete the window.
 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
@@ -812,7 +866,7 @@ bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
     }
 
     SendUnsplitEvent(win);
-    m_sashPosition = 0;
+    DoSetSashPosition(0);
     SizeWindows();
 
     return TRUE;
@@ -844,9 +898,15 @@ bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
     return TRUE;
 }
 
+void wxSplitterWindow::SetMinimumPaneSize(int min)
+{
+    m_minimumPaneSize = min;
+    SetSashPosition(m_sashPosition); // re-check limits
+}
+
 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
 {
-    m_sashPosition = position;
+    DoSetSashPosition(position);
 
     if ( redraw )
     {
@@ -866,20 +926,20 @@ void wxSplitterWindow::InitColours()
 
     // Shadow colours
 #ifndef __WIN16__
-    wxColour faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
+    wxColour faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
     m_facePen = new wxPen(faceColour, 1, wxSOLID);
     m_faceBrush = new wxBrush(faceColour, wxSOLID);
 
-    wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
+    wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
     m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
 
-    wxColour darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW));
+    wxColour darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW));
     m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
 
-    wxColour lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT));
+    wxColour lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
     m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
 
-    wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
+    wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
     m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
 #else
     m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
@@ -911,9 +971,7 @@ void wxSplitterWindow::OnSashPosChanged(wxSplitterEvent& event)
     int newSashPosition = event.GetSashPosition();
 
     // Obtain relevant window dimension for bottom / right threshold check
-    int w, h;
-    GetClientSize(&w, &h);
-    int window_size = (m_splitMode == wxSPLIT_VERTICAL) ? w : h ;
+    int window_size = GetWindowSize();
 
     bool unsplit_scenario = FALSE;
     if ( m_permitUnsplitAlways
@@ -937,10 +995,7 @@ void wxSplitterWindow::OnSashPosChanged(wxSplitterEvent& event)
     if ( !unsplit_scenario )
     {
         // If resultant pane would be too small, enlarge it
-        if ( newSashPosition < m_minimumPaneSize )
-            newSashPosition = m_minimumPaneSize;
-        if ( newSashPosition > window_size - m_minimumPaneSize )
-            newSashPosition = window_size - m_minimumPaneSize;
+        newSashPosition = AdjustSashPosition(newSashPosition);
     }
 
     // If the result is out of bounds it means minimum size is too big,