]> git.saurik.com Git - wxWidgets.git/blobdiff - contrib/src/gizmos/splittree.cpp
don't use wxTheXXXList in wxXXX ctor/dtor, only objects explicitly created
[wxWidgets.git] / contrib / src / gizmos / splittree.cpp
index 83ccea4ec8b74b7e3ba6dd5826731a58d4135c88..1dfd6de5980ead129cded193be918d4c90fef803 100644 (file)
@@ -19,7 +19,7 @@
 // headers
 // ----------------------------------------------------------------------------
 #ifdef __GNUG__
 // headers
 // ----------------------------------------------------------------------------
 #ifdef __GNUG__
-    #pragma interface "splittree.cpp"
+    #pragma implementation "splittree.h"
 #endif
 
 // For compilers that support precompilation, includes "wx/wx.h".
 #endif
 
 // For compilers that support precompilation, includes "wx/wx.h".
     #include "wx/wx.h"
 #endif
 
     #include "wx/wx.h"
 #endif
 
-#include "wx/generic/treectlg.h"
+#ifdef __WXMSW__
+#include <windows.h>
+#include "wx/msw/winundef.h"
+#endif
 
 #include "wx/gizmos/splittree.h"
 
 #include "wx/gizmos/splittree.h"
+#include <math.h>
 
 /*
  * wxRemotelyScrolledTreeCtrl
 
 /*
  * wxRemotelyScrolledTreeCtrl
@@ -55,7 +59,6 @@ BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl, wxGenericTreeCtrl)
 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl, wxTreeCtrl)
 #endif
        EVT_SIZE(wxRemotelyScrolledTreeCtrl::OnSize)
 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl, wxTreeCtrl)
 #endif
        EVT_SIZE(wxRemotelyScrolledTreeCtrl::OnSize)
-       EVT_PAINT(wxRemotelyScrolledTreeCtrl::OnPaint)
        EVT_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand)
        EVT_TREE_ITEM_COLLAPSED(-1, wxRemotelyScrolledTreeCtrl::OnExpand)
        EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll)
        EVT_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand)
        EVT_TREE_ITEM_COLLAPSED(-1, wxRemotelyScrolledTreeCtrl::OnExpand)
        EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll)
@@ -65,6 +68,7 @@ wxRemotelyScrolledTreeCtrl::wxRemotelyScrolledTreeCtrl(wxWindow* parent, wxWindo
         const wxSize& sz, long style):
         wxTreeCtrl(parent, id, pt, sz, style)
 {
         const wxSize& sz, long style):
         wxTreeCtrl(parent, id, pt, sz, style)
 {
+       m_companionWindow = NULL;
 }
 
 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
 }
 
 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
@@ -73,7 +77,7 @@ wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
 
 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
 {
 
 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
 {
-#ifdef __WXMSW__
+#if defined(__WXMSW__) && USE_GENERIC_TREECTRL
     if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     {
         ::ShowScrollBar((HWND) GetHWND(), SB_VERT, FALSE);
     if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     {
         ::ShowScrollBar((HWND) GetHWND(), SB_VERT, FALSE);
@@ -93,6 +97,7 @@ void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX, int pixelsPer
                              int xPos, int yPos,
                              bool noRefresh)
 {
                              int xPos, int yPos,
                              bool noRefresh)
 {
+#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     {
         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     {
         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
@@ -104,15 +109,40 @@ void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX, int pixelsPer
             scrolledWindow->SetScrollbars(0, pixelsPerUnitY, 0, noUnitsY, 0, yPos, noRefresh);
         }
     }
             scrolledWindow->SetScrollbars(0, pixelsPerUnitY, 0, noUnitsY, 0, yPos, noRefresh);
         }
     }
+#endif
+}
+
+// In case we're using the generic tree control.
+int wxRemotelyScrolledTreeCtrl::GetScrollPos(int orient) const
+{
+    wxScrolledWindow* scrolledWindow = GetScrolledWindow();
+
+#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
+    if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
+    {
+        wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
+
+        if (orient == wxHORIZONTAL)
+            return win->wxGenericTreeCtrl::GetScrollPos(orient);
+        else
+        {
+            return scrolledWindow->GetScrollPos(orient);
+        }
+    }
+#endif
+    return 0;
 }
 
 }
 
+
 // In case we're using the generic tree control.
 // Get the view start
 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x, int *y) const
 {
 // In case we're using the generic tree control.
 // Get the view start
 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x, int *y) const
 {
+    wxScrolledWindow* scrolledWindow = GetScrolledWindow();
+
+#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     {
     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     {
-           wxScrolledWindow* scrolledWindow = GetScrolledWindow();
 
         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
         int x1, y1, x2, y2;
 
         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
         int x1, y1, x2, y2;
@@ -124,11 +154,19 @@ void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x, int *y) const
         scrolledWindow->GetViewStart(& x2, & y2);
         * y = y2;
     }
         scrolledWindow->GetViewStart(& x2, & y2);
         * y = y2;
     }
+       else
+#endif
+       {
+               // x is wrong since the horizontal scrollbar is controlled by the
+               // tree control, but we probably don't need it.
+        scrolledWindow->GetViewStart(x, y);
+       }
 }
 
 // In case we're using the generic tree control.
 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC& dc)
 {
 }
 
 // In case we're using the generic tree control.
 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC& dc)
 {
+#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     {
            wxScrolledWindow* scrolledWindow = GetScrolledWindow();
     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     {
            wxScrolledWindow* scrolledWindow = GetScrolledWindow();
@@ -143,8 +181,9 @@ void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC& dc)
         scrolledWindow->GetScrollPixelsPerUnit(& xppu2, & yppu2);
 
         dc.SetDeviceOrigin( -startX * xppu1, -startY * yppu2 );
         scrolledWindow->GetScrollPixelsPerUnit(& xppu2, & yppu2);
 
         dc.SetDeviceOrigin( -startX * xppu1, -startY * yppu2 );
-        dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
+        // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
     }
     }
+#endif
 }
 
 // Scroll to the given line (in scroll units where each unit is
 }
 
 // Scroll to the given line (in scroll units where each unit is
@@ -152,14 +191,19 @@ void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC& dc)
 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz, int posVert)
 {
 #ifdef __WXMSW__
 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz, int posVert)
 {
 #ifdef __WXMSW__
+#if USE_GENERIC_TREECTRL
     if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
     if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
+#endif
     {
            UINT sbCode = SB_THUMBPOSITION;
            HWND vertScrollBar = 0;
            MSWDefWindowProc((WXUINT) WM_VSCROLL, MAKELONG(sbCode, posVert), (WXHWND) vertScrollBar);
     }
     {
            UINT sbCode = SB_THUMBPOSITION;
            HWND vertScrollBar = 0;
            MSWDefWindowProc((WXUINT) WM_VSCROLL, MAKELONG(sbCode, posVert), (WXHWND) vertScrollBar);
     }
+#if USE_GENERIC_TREECTRL
     else
 #endif
     else
 #endif
+#endif
+#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
     {
         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
                win->Refresh();
     {
         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
                win->Refresh();
@@ -173,6 +217,7 @@ void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz, int posVert)
                }
                */
     }
                }
                */
     }
+#endif
 }
 
 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent& event)
 }
 
 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent& event)
@@ -190,54 +235,59 @@ void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent& event)
     // If we don't have this, we get some bits of lines still remaining
     if (event.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED)
         Refresh();
     // If we don't have this, we get some bits of lines still remaining
     if (event.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED)
         Refresh();
+
+       // Pass on the event
+       if (m_companionWindow)
+               m_companionWindow->GetEventHandler()->ProcessEvent(event);
 }
 
 // Adjust the containing wxScrolledWindow's scrollbars appropriately
 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
 {
 }
 
 // Adjust the containing wxScrolledWindow's scrollbars appropriately
 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
 {
-    // WILL THIS BE DONE AUTOMATICALLY BY THE GENERIC TREE CONTROL?
-/*
-
-Problem with remote-scrolling the generic tree control. It relies
-on PrepareDC for adjusting the device origin, which in turn takes
-values from wxScrolledWindow: which we've turned off in order to use
-a different scrollbar :-( So we could override PrepareDC and use
-the _other_ scrolled window's position instead.
-Note also ViewStart would need to be overridden.
-Plus, wxGenericTreeCtrl::OnPaint will reset the device origin.
-
-*/
-
-    // Assumption: wxGenericTreeCtrl will adjust the scrollbars automatically,
-    // since it'll call SetScrollbars and we've defined this to Do The Right Thing.
-    if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
+#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
+    if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
+       {
+               // This is for the generic tree control.
+               // It calls SetScrollbars which has been overridden
+               // to adjust the parent scrolled window vertical
+               // scrollbar.
+               ((wxGenericTreeCtrl*) this)->AdjustMyScrollbars();
         return;
         return;
-
-       wxScrolledWindow* scrolledWindow = GetScrolledWindow();
-       if (scrolledWindow)
+       }
+       else
+#endif
        {
        {
-               wxRect itemRect;
-               if (GetBoundingRect(GetRootItem(), itemRect))
+               // This is for the wxMSW tree control
+               wxScrolledWindow* scrolledWindow = GetScrolledWindow();
+               if (scrolledWindow)
                {
                {
-                       int itemHeight = itemRect.GetHeight();
-
-                       int w, h;
-                       GetClientSize(&w, &h);
-                       
-                       wxRect rect(0, 0, 0, 0);
-                       CalcTreeSize(rect);
-                       int treeViewHeight = rect.GetHeight()/itemHeight;
-                       
-                       int scrollPixelsPerLine = itemHeight;
-                       int scrollPos = - (itemRect.y / itemHeight);
-                       
-                       scrolledWindow->SetScrollbars(0, scrollPixelsPerLine, 0, treeViewHeight, 0, scrollPos);
-                       
-                       // Ensure that when a scrollbar becomes hidden or visible,
-                       // the contained window sizes are right.
-                       // Problem: this is called too early (?)
-                       wxSizeEvent event(scrolledWindow->GetSize(), scrolledWindow->GetId());
-                       scrolledWindow->GetEventHandler()->ProcessEvent(event);
+                       wxRect itemRect;
+                       if (GetBoundingRect(GetRootItem(), itemRect))
+                       {
+                // Actually, the real height seems to be 1 less than reported
+                // (e.g. 16 instead of 16)
+                int itemHeight = itemRect.GetHeight() - 1;
+                               
+                               int w, h;
+                               GetClientSize(&w, &h);
+                               
+                               wxRect rect(0, 0, 0, 0);
+                               CalcTreeSize(rect);
+
+                double f = ((double) (rect.GetHeight()) / (double) itemHeight)  ;
+                int treeViewHeight = (int) ceil(f);
+                               
+                               int scrollPixelsPerLine = itemHeight;
+                               int scrollPos = - (itemRect.y / itemHeight);
+                               
+                               scrolledWindow->SetScrollbars(0, scrollPixelsPerLine, 0, treeViewHeight, 0, scrollPos);
+                               
+                               // Ensure that when a scrollbar becomes hidden or visible,
+                               // the contained window sizes are right.
+                               // Problem: this is called too early (?)
+                               wxSizeEvent event(scrolledWindow->GetSize(), scrolledWindow->GetId());
+                               scrolledWindow->GetEventHandler()->ProcessEvent(event);
+                       }
                }
        }
 }
                }
        }
 }
@@ -271,11 +321,8 @@ void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect& rect)
        CalcTreeSize(GetRootItem(), rect);
 }
 
        CalcTreeSize(GetRootItem(), rect);
 }
 
-void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxTreeItemId& id, wxRect& rect)
+void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId& id, wxRect& rect)
 {
 {
-       // TODO: implement GetFirst/NextVisibleItem
-       // for wxGenericTreeCtrl, plus GetBoundingRect.
-
        // More efficient implementation would be to find the last item (but how?)
        // Q: is the bounding rect relative to the top of the virtual tree workspace
        // or the top of the window? How would we convert?
        // More efficient implementation would be to find the last item (but how?)
        // Q: is the bounding rect relative to the top of the virtual tree workspace
        // or the top of the window? How would we convert?
@@ -307,53 +354,127 @@ wxScrolledWindow* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
        return NULL;
 }
 
        return NULL;
 }
 
-void wxRemotelyScrolledTreeCtrl::OnPaint(wxPaintEvent& event)
+void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent& event)
 {
 {
-       wxPaintDC dc(this);
+    int orient = event.GetOrientation();
+    if (orient == wxHORIZONTAL)
+    {
+        event.Skip();
+        return;
+    }
+    wxScrolledWindow* scrollWin = GetScrolledWindow();
+    if (!scrollWin)
+        return;
 
 
-       wxTreeCtrl::OnPaint(event);
+    int x, y;
+    scrollWin->GetViewStart(& x, & y);
 
 
-    // Reset the device origin since it may have been set
-    dc.SetDeviceOrigin(0, 0);
+    ScrollToLine(-1, y);
+}
 
 
-       wxSize sz = GetClientSize();
+/*
+ * wxTreeCompanionWindow
+ *
+ * A window displaying values associated with tree control items.
+ */
+
+IMPLEMENT_CLASS(wxTreeCompanionWindow, wxWindow)
+
+BEGIN_EVENT_TABLE(wxTreeCompanionWindow, wxWindow)
+       EVT_PAINT(wxTreeCompanionWindow::OnPaint)
+       EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll)
+       EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand)
+       EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand)
+END_EVENT_TABLE()
+
+wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow* parent, wxWindowID id,
+      const wxPoint& pos,
+      const wxSize& sz,
+      long style):
+       wxWindow(parent, id, pos, sz, style)
+{
+       m_treeCtrl = NULL;
+}
 
 
-       wxPen pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
+void wxTreeCompanionWindow::DrawItem(wxDC& dc, wxTreeItemId id, const wxRect& rect)
+{
+       // TEST CODE
+#if 1
+       if (m_treeCtrl)
+       {
+               wxString text = m_treeCtrl->GetItemText(id);
+               dc.SetTextForeground(* wxBLACK);
+               dc.SetBackgroundMode(wxTRANSPARENT);
+
+               int textW, textH;
+               dc.GetTextExtent(text, & textW, & textH);
+
+               int x = 5;
+               int y = rect.GetY() + wxMax(0, (rect.GetHeight() - textH) / 2);
+
+               dc.DrawText(text, x, y);
+       }
+#endif
+}
+
+void wxTreeCompanionWindow::OnPaint(wxPaintEvent& event)
+{
+       wxPaintDC dc(this);
+
+    if (!m_treeCtrl)
+        return;
+
+        wxPen pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
        dc.SetPen(pen);
        dc.SetBrush(* wxTRANSPARENT_BRUSH);
        dc.SetPen(pen);
        dc.SetBrush(* wxTRANSPARENT_BRUSH);
+       wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
+       dc.SetFont(font);
 
 
+    wxSize clientSize = GetClientSize();
        wxRect itemRect;
        wxRect itemRect;
-       if (GetBoundingRect(GetRootItem(), itemRect))
+       int cy=0;
+       wxTreeItemId h, lastH;
+       for(h=m_treeCtrl->GetFirstVisibleItem();h;h=m_treeCtrl->GetNextVisible(h))
        {
        {
-               int itemHeight = itemRect.GetHeight();
-               wxRect rcClient = GetRect();
-               int cy=0;
-               wxTreeItemId h;
-               for(h=GetFirstVisibleItem();h;h=GetNextVisible(h))
+               if (m_treeCtrl->GetBoundingRect(h, itemRect))
                {
                {
-                       dc.DrawLine(rcClient.x, cy, rcClient.x + rcClient.width, cy);
-                       cy += itemHeight;
+                       cy = itemRect.GetTop();
+                       wxRect drawItemRect(0, cy, clientSize.x, itemRect.GetHeight());
+
+                       lastH = h;
+
+                       // Draw the actual item
+                       DrawItem(dc, h, drawItemRect);
+                       dc.DrawLine(0, cy, clientSize.x, cy);
                }
                }
-               dc.DrawLine(rcClient.x, cy, rcClient.x + rcClient.width, cy);
+       }
+       if (lastH.IsOk() && m_treeCtrl->GetBoundingRect(lastH, itemRect))
+       {
+               cy = itemRect.GetBottom();
+               dc.DrawLine(0, cy, clientSize.x, cy);
        }
 }
 
        }
 }
 
-void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent& event)
+void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent& event)
 {
     int orient = event.GetOrientation();
     if (orient == wxHORIZONTAL)
     {
 {
     int orient = event.GetOrientation();
     if (orient == wxHORIZONTAL)
     {
-        // Don't 'skip' or we'd get into infinite recursion
+        event.Skip();
         return;
     }
         return;
     }
-    wxScrolledWindow* scrollWin = GetScrolledWindow();
-    if (!scrollWin)
+    if (!m_treeCtrl)
         return;
 
         return;
 
-    int x, y;
-    scrollWin->GetViewStart(& x, & y);
+       // TODO: scroll the window physically instead of just refreshing.
+       Refresh(TRUE);
+}
 
 
-    ScrollToLine(-1, y);
+void wxTreeCompanionWindow::OnExpand(wxTreeEvent& event)
+{
+       // TODO: something more optimized than simply refresh the whole
+       // window when the tree is expanded/collapsed. Tricky.
+       Refresh();
 }
 
 /*
 }
 
 /*
@@ -471,9 +592,12 @@ void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent& event)
     // don't cause an infinite loop
     static bool inOnScroll = FALSE;
     if (inOnScroll)
     // don't cause an infinite loop
     static bool inOnScroll = FALSE;
     if (inOnScroll)
+    {
+        event.Skip();
         return;
         return;
+    }
     inOnScroll = TRUE;
     inOnScroll = TRUE;
-
+    
     int orient = event.GetOrientation();
 
     int nScrollInc = CalcScrollInc(event);
     int orient = event.GetOrientation();
 
     int nScrollInc = CalcScrollInc(event);
@@ -485,8 +609,13 @@ void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent& event)
 
     if (orient == wxHORIZONTAL)
     {
 
     if (orient == wxHORIZONTAL)
     {
+        inOnScroll = FALSE;
+        event.Skip();
+        return;
+#if 0
         int newPos = m_xScrollPosition + nScrollInc;
         SetScrollPos(wxHORIZONTAL, newPos, TRUE );
         int newPos = m_xScrollPosition + nScrollInc;
         SetScrollPos(wxHORIZONTAL, newPos, TRUE );
+#endif
     }
     else
     {
     }
     else
     {