From 0bd2681966523df88ad5cf8e505b532843e58d74 Mon Sep 17 00:00:00 2001
From: Robert Roebling <robert@roebling.de>
Date: Wed, 22 Aug 2007 21:18:06 +0000
Subject: [PATCH] Added logic, API and docs for
 wxDataViewModel::HasDefaultCompare indicating a compare function usable
 without and column (header)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48345 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/dataviewcolumn.tex | 11 ++++++++---
 docs/latex/wx/dataviewmodel.tex  | 18 +++++++++++++++++-
 include/wx/dataview.h            |  2 ++
 samples/dataview/dataview.cpp    | 19 +++++++++++++++++--
 src/gtk/dataview.cpp             | 19 +++++++++++++------
 5 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/docs/latex/wx/dataviewcolumn.tex b/docs/latex/wx/dataviewcolumn.tex
index bf8e50e407..ef2dd7c0d7 100644
--- a/docs/latex/wx/dataviewcolumn.tex
+++ b/docs/latex/wx/dataviewcolumn.tex
@@ -128,9 +128,14 @@ a little arrow.
 
 \func{void}{SetSortable}{\param{bool }{sortable}}
 
-Indicate that the column is sortable. This is only to provide a
-visual hint in the column (such as a sort order indicator). It
-will not actually sort the data. 
+Indicate that the column is sortable. This does
+not show any sorting indicate yet, but it does
+make the column header clickable. Call 
+\helpref{SetSortOrder}{wxdataviewcolumnsetsortorder}
+afterwards to actually make the sort indicator appear.
+If {\it sortable} is false, the column header is
+no longer clickable and the sort indicator (little
+arrow) will disappear.
 
 \membersection{wxDataViewColumn::SetTitle}\label{wxdataviewcolumnsettitle}
 
diff --git a/docs/latex/wx/dataviewmodel.tex b/docs/latex/wx/dataviewmodel.tex
index 8b00219057..0fc1425b62 100644
--- a/docs/latex/wx/dataviewmodel.tex
+++ b/docs/latex/wx/dataviewmodel.tex
@@ -38,7 +38,9 @@ Note that wxDataViewModel does not define the position or
 index of any item in the control since several control might
 display the data differently, but wxDataViewModel does
 provide a \helpref{Compare}{wxdataviewmodelcompare} method
-which the wxDataViewCtrl may use to sort the data.
+which the wxDataViewCtrl may use to sort the data either
+in conjunction with a column header or without (see
+\helpref{HasDefaultCompare}{wxdataviewmodelhasdefaultcompare}.
 
 This class maintains a list of 
 \helpref{wxDataViewModelNotifier}{wxdataviewmodelnotifier}
@@ -107,6 +109,8 @@ The compare function to be used by control. The default compare function
 sorts by container and other items separately and in ascending order.
 Override this for a different sorting behaviour.
 
+See also \helpref{HasDefaultCompare}{wxdataviewmodelhasdefaultcompare}.
+
 \membersection{wxDataViewModel::GetColumnCount}\label{wxdataviewmodelgetcolumncount}
 
 \constfunc{unsigned int}{GetColumnCount}{\void}
@@ -147,6 +151,18 @@ of {\it item} or an invalid wxDataViewItem if {\it item} is the root item.
 Override this to indicate the value of {\it item}
 A \helpref{wxVariant}{wxvariant} is used to store the data.
 
+\membersection{wxDataViewModel::HasDefaultCompare}\label{wxdataviewmodelhasdefaultcompare}
+
+\func{bool}{HasDefaultCompare}{\void}
+
+Override this to indicate that the model provides a default compare
+function that the control should use if no wxDataViewColumn has been
+chosen for sorting. Usually, the user clicks on a column header for
+sorting, the data will be sorted alphanumerically. If any other
+order (e.g. by index or order of appearance) is required, then this
+should be used. See also \helpref{wxDataViewIndexListModel}{wxdataviewindexlistmodel}
+for a model which makes use of this.
+
 \membersection{wxDataViewModel::IsContainer}\label{wxdataviewmodeliscontainer}
 
 \constfunc{bool}{IsContainer}{\param{const wxDataViewItem\& }{item}}
diff --git a/include/wx/dataview.h b/include/wx/dataview.h
index 2286965603..55e3d1e944 100644
--- a/include/wx/dataview.h
+++ b/include/wx/dataview.h
@@ -156,6 +156,7 @@ public:
     // default compare function
     virtual int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, 
                          unsigned int column, bool ascending );
+    virtual bool HasDefaultCompare() { return false; }
 
 protected:
     // the user should not delete this class directly: he should use DecRef() instead!
@@ -198,6 +199,7 @@ public:
     
     virtual int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, 
                          unsigned int column, bool ascending );
+    virtual bool HasDefaultCompare() { return true; }
 
     // implement base methods
 
diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp
index 694cb30d8b..09b104861c 100644
--- a/samples/dataview/dataview.cpp
+++ b/samples/dataview/dataview.cpp
@@ -221,7 +221,16 @@ public:
             case 0: variant = node->m_title; break;
             case 1: variant = node->m_artist; break;
             case 2: variant = node->m_year; break;
-            default: wxLogError( "MyMusicModel::GetValue: wrong column" );
+            default: 
+            {
+                wxLogError( "MyMusicModel::GetValue: wrong column" );
+                wxMouseState state = wxGetMouseState();
+                if (state.ShiftDown())
+                {
+                    char *crash = 0;
+                    *crash = 0;
+                }
+            }
         }
     }
 
@@ -558,8 +567,14 @@ MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
     m_music_model = new MyMusicModel;
     m_musicCtrl->AssociateModel( m_music_model.get() );
 
-    m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200, 
+    wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200, 
                                      DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
+#if 0 
+    // Call this and sorting is enabled
+    // immediatly upon start up.                                    
+    col->SetSortOrder( true );
+#endif
+    
     m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 200,
                                      DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
     m_musicCtrl->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50,
diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp
index 0e9f57dc49..06836322f2 100644
--- a/src/gtk/dataview.cpp
+++ b/src/gtk/dataview.cpp
@@ -157,28 +157,28 @@ public:
             void *id = child->GetItem().GetID();
             
             m_children.Add( id );
-#if 0            
-            if (m_internal->IsSorted())
+
+            if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
             {
                 g_internal = m_internal;
                 m_children.Sort( &wxGtkTreeModelChildCmp );
                 return m_children.Index( id );
             }
-#endif
+
             return m_children.GetCount()-1;
         }
         
     unsigned int AddLeave( void* id )
         {
             m_children.Add( id );
-#if 0            
-            if (m_internal->IsSorted())
+
+            if (m_internal->IsSorted() || m_internal->GetDataViewModel()->HasDefaultCompare())
             {
                 g_internal = m_internal;
                 m_children.Sort( &wxGtkTreeModelChildCmp );
                 return m_children.Index( id );
             }
-#endif
+
             return m_children.GetCount()-1;
         }
         
@@ -2097,9 +2097,14 @@ void wxDataViewColumn::SetSortable( bool sortable )
     GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
     
     if (sortable)
+    {
         gtk_tree_view_column_set_sort_column_id( column, GetModelColumn() );
+    }
     else
+    {
         gtk_tree_view_column_set_sort_column_id( column, -1 );
+        gtk_tree_view_column_set_sort_indicator( column, FALSE );
+    }
 }
 
 bool wxDataViewColumn::IsSortable() const
@@ -2128,6 +2133,8 @@ void wxDataViewColumn::SetSortOrder( bool ascending )
         gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
     else
         gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
+
+    gtk_tree_view_column_set_sort_indicator( column, TRUE );
 }
 
 bool wxDataViewColumn::IsSortOrderAscending() const
-- 
2.50.0

