X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8f850e28314ba5c1a020b3853b71f3e8520b04fe..cb0b7b7d811356f729315fc14c7e0d311f43384d:/src/common/datavcmn.cpp diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index 6d9a39d736..268856c151 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -142,6 +142,22 @@ bool wxDataViewListModel::ValueChanged( size_t col, size_t row ) return ret; } +bool wxDataViewListModel::RowsReordered( size_t *new_order ) +{ + bool ret = true; + + wxNode *node = m_notifiers.GetFirst(); + while (node) + { + wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData(); + if (!notifier->RowsReordered( new_order )) + ret = false; + node = node->GetNext(); + } + + return ret; +} + bool wxDataViewListModel::Cleared() { bool ret = true; @@ -192,14 +208,38 @@ void wxDataViewListModel::RemoveNotifier( wxDataViewListModelNotifier *notifier } // --------------------------------------------------------- -// wxDataViewSortedListModel +// wxDataViewSortedListModelNotifier +// --------------------------------------------------------- + +class wxDataViewSortedListModelNotifier: public wxDataViewListModelNotifier +{ +public: + wxDataViewSortedListModelNotifier( wxDataViewSortedListModel *model ) + { m_model = model; } + + virtual bool RowAppended() { return true; } + virtual bool RowPrepended() { return true; } + virtual bool RowInserted( size_t before ) { return true; } + virtual bool RowDeleted( size_t row ) { return true; } + virtual bool RowChanged( size_t row ) { return true; } + virtual bool ValueChanged( size_t col, size_t row ) + { return m_model->ChildValueChanged( col, row); } + virtual bool RowsReordered( size_t *new_order ) { return true; } + virtual bool Cleared() { return true; } + + wxDataViewSortedListModel *m_model; +}; + +// --------------------------------------------------------- +// wxDataViewSortedListModel compare function // --------------------------------------------------------- int wxCALLBACK wxDataViewListModelSortedDefaultCompare (size_t row1, size_t row2, size_t col, wxDataViewListModel* model ) { - wxVariant value1 = model->GetValue( col, row1 ); - wxVariant value2 = model->GetValue( col, row2 ); + wxVariant value1,value2; + model->GetValue( value1, col, row1 ); + model->GetValue( value2, col, row2 ); if (value1.GetType() == wxT("string")) { wxString str1 = value1.GetString(); @@ -241,6 +281,9 @@ int LINKAGEMODE wxDataViewIntermediateCmp( size_t row1, size_t row2 ) return s_CmpFunc( row1, row2, s_CmpCol, s_CmpModel ); } +// --------------------------------------------------------- +// wxDataViewSortedListModel +// --------------------------------------------------------- IMPLEMENT_ABSTRACT_CLASS(wxDataViewSortedListModel, wxDataViewListModel) @@ -252,11 +295,15 @@ wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel *child s_CmpModel = child; s_CmpFunc = wxDataViewListModelSortedDefaultCompare; + m_notifierOnChild = new wxDataViewSortedListModelNotifier( this ); + m_child->AddNotifier( m_notifierOnChild ); + Resort(); } wxDataViewSortedListModel::~wxDataViewSortedListModel() { + m_child->RemoveNotifier( m_notifierOnChild ); } void wxDataViewSortedListModel::Resort() @@ -268,9 +315,79 @@ void wxDataViewSortedListModel::Resort() m_array.Add( i ); } +#if 0 +static void Dump( wxDataViewListModel *model, size_t col ) +{ + size_t n = model->GetNumberOfRows(); + size_t i; + for (i = 0; i < n; i++) + { + wxVariant variant; + model->GetValue( variant, col, i ); + wxString tmp; + tmp = variant.GetString(); + wxPrintf( wxT("%d: %s\n"), (int) i, tmp.c_str() ); + } +} +#endif + +bool wxDataViewSortedListModel::ChildValueChanged( size_t col, size_t row ) +{ + size_t i; + size_t len = m_array.GetCount(); + + // Remove and readd sorted. Find out at which + // position it was and where it ended. + size_t start_pos = 0,end_pos = 0; + for (i = 0; i < len; i++) + if (m_array[i] == row) + { + start_pos = i; + break; + } + m_array.RemoveAt( start_pos ); + m_array.Add( row ); + + for (i = 0; i < len; i++) + if (m_array[i] == row) + { + end_pos = i; + break; + } + + if (end_pos == start_pos) + return wxDataViewListModel::ValueChanged( col, start_pos ); + + // Create an array where order[old] -> new_pos, so that + // if nothing changed order[0] -> 0 etc. + size_t *order = new size_t[ len ]; + // Fill up initial values. + for (i = 0; i < len; i++) + order[i] = i; + + if (start_pos < end_pos) + { + for (i = start_pos; i < end_pos; i++) + order[i] = order[i+1]; + order[end_pos] = start_pos; + } + else + { + for (i = end_pos; i > start_pos; i--) + order[i] = order[i-1]; + order[start_pos] = end_pos; + } + + wxDataViewListModel::RowsReordered( order ); + + delete [] order; + + return true; +} + size_t wxDataViewSortedListModel::GetNumberOfRows() { - return m_child->GetNumberOfRows(); + return m_array.GetCount(); } size_t wxDataViewSortedListModel::GetNumberOfCols() @@ -283,17 +400,19 @@ wxString wxDataViewSortedListModel::GetColType( size_t col ) return m_child->GetColType( col ); } -wxVariant wxDataViewSortedListModel::GetValue( size_t col, size_t row ) +void wxDataViewSortedListModel::GetValue( wxVariant &variant, size_t col, size_t row ) { size_t child_row = m_array[row]; - return m_child->GetValue( col, child_row ); + m_child->GetValue( variant, col, child_row ); } bool wxDataViewSortedListModel::SetValue( wxVariant &variant, size_t col, size_t row ) { size_t child_row = m_array[row]; bool ret = m_child->SetValue( variant, col, child_row ); - // resort in ::ValueChanged() + + // Resort in ::ChildValueChanged() which gets reported back. + return ret; } @@ -333,7 +452,8 @@ bool wxDataViewSortedListModel::RowDeleted( size_t row ) bool ret = m_child->RowDeleted( child_row ); - wxDataViewListModel::RowDeleted( row ); + // Do nothing here as the change in the + // child model will be reported back. return ret; } @@ -343,7 +463,8 @@ bool wxDataViewSortedListModel::RowChanged( size_t row ) size_t child_row = m_array[row]; bool ret = m_child->RowChanged( child_row ); - // report delete old pos, inserted new pos + // Do nothing here as the change in the + // child model will be reported back. return ret; } @@ -353,12 +474,19 @@ bool wxDataViewSortedListModel::ValueChanged( size_t col, size_t row ) size_t child_row = m_array[row]; bool ret = m_child->ValueChanged( col, child_row ); - // Do nothing if not the sorted col.. - // report delete old pos, inserted new pos + // Do nothing here as the change in the + // child model will be reported back. return ret; } +bool wxDataViewSortedListModel::RowsReordered( size_t *new_order ) +{ + // We sort them ourselves. + + return false; +} + bool wxDataViewSortedListModel::Cleared() { bool ret = m_child->Cleared(); @@ -431,15 +559,10 @@ wxDataViewCtrlBase::wxDataViewCtrlBase() wxDataViewCtrlBase::~wxDataViewCtrlBase() { - if (m_model) - delete m_model; } bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel *model ) { - if (m_model) - delete m_model; - m_model = model; return true;