]> git.saurik.com Git - wxWidgets.git/commitdiff
restore sorting functionality of the generic wxDataViewCtrl but implement it on top...
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 8 Dec 2008 21:13:29 +0000 (21:13 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 8 Dec 2008 21:13:29 +0000 (21:13 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57206 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/generic/dataview.h
src/generic/datavgen.cpp

index 953c2931427478ebdfa2376f7fc3eec5b47855f7..e26bb1043488109e5e8d484e258daedc146e340c 100644 (file)
@@ -463,8 +463,8 @@ protected:
     virtual wxDataViewItem GetItemByRow( unsigned int row ) const;
     virtual int GetRowByItem( const wxDataViewItem & item ) const;
 
-    wxDataViewColumn* GetSortingColumn() { return m_sortingColumn; }
-    void SetSortingColumn( wxDataViewColumn* column ) { m_sortingColumn = column; }
+    int GetSortingColumnIndex() const { return m_sortingColumnIdx; }
+    void SetSortingColumnIndex(int idx) { m_sortingColumnIdx = idx; }
 
 public:     // utility functions not part of the API
 
@@ -490,7 +490,9 @@ private:
     wxDataViewModelNotifier  *m_notifier;
     wxDataViewMainWindow     *m_clientArea;
     wxDataViewHeaderWindow   *m_headerArea;
-    wxDataViewColumn         *m_sortingColumn;
+
+    // the index of the column currently used for sorting or -1
+    int m_sortingColumnIdx;
 
 private:
     void OnSize( wxSizeEvent &event );
index 2cda56e24c5f66321cbc3604116ff73a7c5c02c2..356337231302342a8d67e8525ccf639831835ddc 100644 (file)
@@ -125,9 +125,46 @@ private:
 
     void OnClick(wxHeaderCtrlEvent& event)
     {
-        if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK,
-                        event.GetColumn()) )
+        const unsigned idx = event.GetColumn();
+
+        if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, idx) )
+            return;
+
+        // default handling for the column click is to sort by this column or
+        // toggle its sort order
+        wxDataViewCtrl * const owner = GetOwner();
+        wxDataViewColumn * const col = owner->GetColumn(idx);
+        if ( !col->IsSortable() )
+        {
+            // no default handling for non-sortable columns
             event.Skip();
+            return;
+        }
+
+        if ( col->IsSortKey() )
+        {
+            // already using this column for sorting, just change the order
+            col->ToggleSortOrder();
+        }
+        else // not using this column for sorting yet
+        {
+            // first unset the old sort column if any
+            int oldSortKey = owner->GetSortingColumnIndex();
+            if ( oldSortKey != wxNOT_FOUND )
+            {
+                owner->GetColumn(oldSortKey)->UnsetAsSortKey();
+                owner->OnColumnChange(oldSortKey);
+            }
+
+            owner->SetSortingColumnIndex(idx);
+            col->SetAsSortKey();
+        }
+
+        wxDataViewModel * const model = owner->GetModel();
+        if ( model )
+            model->Resort();
+
+        owner->OnColumnChange(idx);
     }
 
     void OnRClick(wxHeaderCtrlEvent& event)
@@ -3188,7 +3225,8 @@ void wxDataViewCtrl::Init()
     m_notifier = NULL;
 
     // No sorting column at start
-    m_sortingColumn = NULL;
+    m_sortingColumnIdx = wxNOT_FOUND;
+
     m_headerArea = NULL;
 }
 
@@ -3414,7 +3452,8 @@ int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
 
 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
 {
-    return NULL;
+    return m_sortingColumnIdx == wxNOT_FOUND ? NULL
+                                             : GetColumn(m_sortingColumnIdx);
 }
 
 //Selection code with wxDataViewItem as parameters