From 91a6c655582321b61a7545ed9c121ad6593ccbfe Mon Sep 17 00:00:00 2001
From: Robert Roebling <robert@roebling.de>
Date: Sun, 26 Aug 2007 10:05:16 +0000
Subject: [PATCH] Move column organizing code to ports, away from common code

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48391 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/dataviewctrl.tex |  6 ++--
 include/wx/dataview.h          | 13 ++++----
 include/wx/gtk/dataview.h      |  8 +++++
 src/common/datavcmn.cpp        | 30 -----------------
 src/gtk/dataview.cpp           | 59 ++++++++++++++++++++++++++++++++--
 5 files changed, 75 insertions(+), 41 deletions(-)

diff --git a/docs/latex/wx/dataviewctrl.tex b/docs/latex/wx/dataviewctrl.tex
index 8699ae2ea8..8e5057139e 100644
--- a/docs/latex/wx/dataviewctrl.tex
+++ b/docs/latex/wx/dataviewctrl.tex
@@ -182,7 +182,7 @@ Collapses the item.
     
 \membersection{wxDataViewCtrl::DeleteColumn}\label{wxdataviewctrldeletecolumn}
 
-\func{virtual bool}{DeleteColumn}{\param{unsigned int }{pos}}
+\func{virtual bool}{DeleteColumn}{\param{const wxDataViewColumn* }{column}}
 
 Deletes given column.
 
@@ -196,7 +196,9 @@ Expands the item.
 
 \constfunc{virtual wxDataViewColumn*}{GetColumn}{\param{unsigned int }{pos}}
 
-Returns pointer to the column.
+Returns pointer to the column. {\it pos} refers to the 
+position in the control which may change after reordering
+columns by the user.
 
 \membersection{wxDataViewCtrl::GetModel}\label{wxdataviewctrlgetmodel}
 
diff --git a/include/wx/dataview.h b/include/wx/dataview.h
index a7e6fc22f0..3f9309bcb5 100644
--- a/include/wx/dataview.h
+++ b/include/wx/dataview.h
@@ -459,12 +459,12 @@ public:
     
     virtual bool AppendColumn( wxDataViewColumn *col );
 
-    virtual unsigned int GetColumnCount() const;
-
-    virtual bool DeleteColumn( unsigned int pos );
-    virtual bool ClearColumns();
-    virtual wxDataViewColumn* GetColumn( unsigned int pos ) const;
-
+    virtual unsigned int GetColumnCount() const = 0;
+    virtual wxDataViewColumn* GetColumn( unsigned int pos ) const = 0;
+    
+    virtual bool DeleteColumn( wxDataViewColumn *column ) = 0;
+    virtual bool ClearColumns() = 0;
+    
     void SetExpanderColumn( wxDataViewColumn *col )
         { m_expander_column = col ; DoSetExpanderColumn(); }
     wxDataViewColumn *GetExpanderColumn() const 
@@ -499,7 +499,6 @@ protected:
 
 private:
     wxDataViewModel        *m_model;
-    wxList                  m_cols;
     wxDataViewColumn       *m_expander_column;
     int m_indent ;
 	
diff --git a/include/wx/gtk/dataview.h b/include/wx/gtk/dataview.h
index b84a7f4f8a..e4df649926 100644
--- a/include/wx/gtk/dataview.h
+++ b/include/wx/gtk/dataview.h
@@ -275,6 +275,8 @@ protected:
     DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewColumn)
 };
 
+WX_DECLARE_LIST(wxDataViewColumn, wxDataViewColumnList );
+
 // --------------------------------------------------------- 
 // wxDataViewCtrl
 // --------------------------------------------------------- 
@@ -305,7 +307,12 @@ public:
            const wxValidator& validator = wxDefaultValidator );
 
     virtual bool AssociateModel( wxDataViewModel *model );
+    
     virtual bool AppendColumn( wxDataViewColumn *col );
+    virtual unsigned int GetColumnCount() const;
+    virtual wxDataViewColumn* GetColumn( unsigned int pos ) const;
+    virtual bool DeleteColumn( wxDataViewColumn *column );
+    virtual bool ClearColumns();
 
     virtual wxDataViewItem GetSelection() const;
     virtual int GetSelections( wxDataViewItemArray & sel ) const;
@@ -346,6 +353,7 @@ private:
     GtkWidget               *m_treeview;
     wxDataViewModelNotifier *m_notifier;
     wxDataViewCtrlInternal  *m_internal;
+    wxDataViewColumnList     m_cols;
 
     
     virtual void OnInternalIdle();
diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp
index a3e934ab35..5ecd70e872 100644
--- a/src/common/datavcmn.cpp
+++ b/src/common/datavcmn.cpp
@@ -539,18 +539,12 @@ IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
 wxDataViewCtrlBase::wxDataViewCtrlBase()
 {
     m_model = NULL;
-    m_cols.DeleteContents( true );
     m_expander_column = 0;
     m_indent = 8;
 }
 
 wxDataViewCtrlBase::~wxDataViewCtrlBase()
 {
-    // IMPORTANT: before calling DecRef() on our model (since it may
-    //            result in a free() call), erase all columns (since
-    //            they hold a pointer to our model)
-    m_cols.Clear();
-
     if (m_model)
     {
         m_model->DecRef();
@@ -694,34 +688,10 @@ wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int mode
 bool
 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
 {
-    m_cols.Append( (wxObject*) col );
     col->SetOwner( (wxDataViewCtrl*) this );
     return true;
 }
 
-unsigned int wxDataViewCtrlBase::GetColumnCount() const
-{
-    return m_cols.GetCount();
-}
-
-bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos) )
-{
-    return false;
-}
-
-bool wxDataViewCtrlBase::ClearColumns()
-{
-    return false;
-}
-
-wxDataViewColumn* wxDataViewCtrlBase::GetColumn( unsigned int pos ) const
-{
-    if( pos >= m_cols.GetCount() )
-        return NULL;
-
-    return (wxDataViewColumn*) m_cols[ pos ];
-}
-
 // ---------------------------------------------------------
 // wxDataViewEvent
 // ---------------------------------------------------------
diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp
index e4111bc0db..60b0ec0791 100644
--- a/src/gtk/dataview.cpp
+++ b/src/gtk/dataview.cpp
@@ -1977,6 +1977,9 @@ static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
 
 IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
 
+#include <wx/listimpl.cpp>
+WX_DEFINE_LIST(wxDataViewColumnList);
+
 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
                                     unsigned int model_column, int width,
                                     wxAlignment align, int flags ) :
@@ -2986,13 +2989,65 @@ bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
     if (!wxDataViewCtrlBase::AppendColumn(col))
         return false;
 
-    GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle();
+    m_cols.Append( col );
+
+    gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), 
+                                 GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
+
+    return true;
+}
+
+unsigned int wxDataViewCtrl::GetColumnCount() const
+{
+    return m_cols.GetCount();
+}
+
+wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
+{
+    GtkTreeViewColumn *gtk_col = gtk_tree_view_get_column( GTK_TREE_VIEW(m_treeview), pos );
+    if (!gtk_col)
+        return NULL;
+        
+    wxDataViewColumnList::const_iterator iter;
+    for (iter = m_cols.begin(); iter != m_cols.end(); iter++)
+    {
+        wxDataViewColumn *col = *iter;
+        if (GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) == gtk_col)
+        {
+            return col;
+        }
+    }
+    
+    return NULL;
+}
+
+bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
+{
+    gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview), 
+                                 GTK_TREE_VIEW_COLUMN(column->GetGtkHandle()) );
+
+    m_cols.remove( column );
 
-    gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column );
+    delete column;
 
     return true;
 }
 
+bool wxDataViewCtrl::ClearColumns()
+{
+    wxDataViewColumnList::iterator iter;
+    for (iter = m_cols.begin(); iter != m_cols.end(); iter++)
+    {
+        wxDataViewColumn *col = *iter;
+        gtk_tree_view_remove_column( GTK_TREE_VIEW(m_treeview), 
+                                     GTK_TREE_VIEW_COLUMN(col->GetGtkHandle()) );
+    }
+    
+    m_cols.clear();
+    
+    return true;
+}
+
 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
 {
     GtkTreeIter iter;
-- 
2.50.0

