From 46234a038344f125655e5d27a0ed0e66202298d4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 8 Dec 2008 21:13:29 +0000 Subject: [PATCH] restore sorting functionality of the generic wxDataViewCtrl but implement it on top of wxHeaderCtrl support for it now; don't have const and non-const overloads of GetSortingColumn() in wxDVC returning different things (could this have really been intentional?); added GetSortingColumnIndex() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57206 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/dataview.h | 8 +++--- src/generic/datavgen.cpp | 47 ++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/include/wx/generic/dataview.h b/include/wx/generic/dataview.h index 953c293142..e26bb10434 100644 --- a/include/wx/generic/dataview.h +++ b/include/wx/generic/dataview.h @@ -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 ); diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 2cda56e24c..3563372313 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -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 -- 2.45.2