X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/07a84e7bbb3ed346fff59aa3570a1f687a3c4d7b..d3ae69de983f61c1ede821e32c18ae3a8bd8c70c:/src/common/datavcmn.cpp diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index 976723b2f4..c3aba3e3d4 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -25,18 +25,11 @@ const wxChar wxDataViewCtrlNameStr[] = wxT("dataviewCtrl"); -// --------------------------------------------------------- -// wxDataViewModel -// --------------------------------------------------------- - -IMPLEMENT_ABSTRACT_CLASS(wxDataViewModel, wxObject) // --------------------------------------------------------- // wxDataViewListModel // --------------------------------------------------------- -IMPLEMENT_ABSTRACT_CLASS(wxDataViewListModel, wxDataViewModel) - wxDataViewListModel::wxDataViewListModel() { m_viewingColumns.DeleteContents( true ); @@ -217,6 +210,9 @@ class wxDataViewSortedListModelNotifier: public wxDataViewListModelNotifier public: wxDataViewSortedListModelNotifier( wxDataViewSortedListModel *model ) { m_model = model; } + + ~wxDataViewSortedListModelNotifier() + { m_model->DetachChild(); } virtual bool RowAppended() { return m_model->ChildRowAppended(); } @@ -287,9 +283,15 @@ int wxCALLBACK wxDataViewListModelSortedDefaultCompare return 0; } +int wxCALLBACK wxDataViewListModelSortedDefaultCompareDescending + (unsigned int row1, unsigned int row2, unsigned int col, wxDataViewListModel* model ) +{ + return wxDataViewListModelSortedDefaultCompare( row2, row1, col, model ); +} + static wxDataViewListModelCompare s_CmpFunc; static wxDataViewListModel *s_CmpModel; -static unsigned int s_CmpCol; +static unsigned int s_CmpCol; int LINKAGEMODE wxDataViewIntermediateCmp( unsigned int row1, unsigned int row2 ) { @@ -300,15 +302,12 @@ int LINKAGEMODE wxDataViewIntermediateCmp( unsigned int row1, unsigned int row2 // wxDataViewSortedListModel // --------------------------------------------------------- -IMPLEMENT_ABSTRACT_CLASS(wxDataViewSortedListModel, wxDataViewListModel) - wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel *child ) : m_array( wxDataViewIntermediateCmp ) { m_child = child; - s_CmpCol = 0; - s_CmpModel = child; - s_CmpFunc = wxDataViewListModelSortedDefaultCompare; + + m_ascending = true; m_notifierOnChild = new wxDataViewSortedListModelNotifier( this ); m_child->AddNotifier( m_notifierOnChild ); @@ -318,22 +317,44 @@ wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel *child wxDataViewSortedListModel::~wxDataViewSortedListModel() { - m_child->RemoveNotifier( m_notifierOnChild ); + if (m_child) + m_child->RemoveNotifier( m_notifierOnChild ); +} + +void wxDataViewSortedListModel::DetachChild() +{ + m_child = NULL; +} + +// FIXME +void wxDataViewSortedListModel::InitStatics() +{ + s_CmpCol = 0; + s_CmpModel = m_child; + if (m_ascending) + s_CmpFunc = wxDataViewListModelSortedDefaultCompare; + else + s_CmpFunc = wxDataViewListModelSortedDefaultCompareDescending; } void wxDataViewSortedListModel::Resort() { + InitStatics(); + m_array.Clear(); - unsigned int n = m_child->GetNumberOfRows(); + unsigned int n = m_child->GetRowCount(); unsigned int i; for (i = 0; i < n; i++) m_array.Add( i ); + + // do we need the neworder? + wxDataViewListModel::RowsReordered( NULL ); } #if 0 static void Dump( wxDataViewListModel *model, unsigned int col ) { - unsigned int n = model->GetNumberOfRows(); + unsigned int n = model->GetRowCount(); unsigned int i; for (i = 0; i < n; i++) { @@ -559,28 +580,28 @@ bool wxDataViewSortedListModel::ChildCleared() return wxDataViewListModel::Cleared(); } -unsigned int wxDataViewSortedListModel::GetNumberOfRows() +unsigned int wxDataViewSortedListModel::GetRowCount() const { return m_array.GetCount(); } -unsigned int wxDataViewSortedListModel::GetNumberOfCols() +unsigned int wxDataViewSortedListModel::GetColumnCount() const { - return m_child->GetNumberOfCols(); + return m_child->GetColumnCount(); } -wxString wxDataViewSortedListModel::GetColType( unsigned int col ) +wxString wxDataViewSortedListModel::GetColumnType( unsigned int col ) const { - return m_child->GetColType( col ); + return m_child->GetColumnType( col ); } -void wxDataViewSortedListModel::GetValue( wxVariant &variant, unsigned int col, unsigned int row ) +void wxDataViewSortedListModel::GetValue( wxVariant &variant, unsigned int col, unsigned int row ) const { unsigned int child_row = m_array[row]; m_child->GetValue( variant, col, child_row ); } -bool wxDataViewSortedListModel::SetValue( wxVariant &variant, unsigned int col, unsigned int row ) +bool wxDataViewSortedListModel::SetValue( const wxVariant &variant, unsigned int col, unsigned int row ) { unsigned int child_row = m_array[row]; bool ret = m_child->SetValue( variant, col, child_row ); @@ -681,10 +702,134 @@ bool wxDataViewSortedListModel::Cleared() IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject) -wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype, wxDataViewCellMode mode ) +wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype, + wxDataViewCellMode WXUNUSED(mode), + int WXUNUSED(align) ) { m_variantType = varianttype; - m_mode = mode; + m_editorCtrl = NULL; + m_row = (unsigned int) -1; +} + +const wxDataViewCtrl* wxDataViewRendererBase::GetView() const +{ + return wx_const_cast(wxDataViewRendererBase*, this)->GetOwner()->GetOwner(); +} + +bool wxDataViewRendererBase::StartEditing( unsigned int row, wxRect labelRect ) +{ + m_row = row; // remember for later + + unsigned int col = GetOwner()->GetModelColumn(); + wxVariant value; + GetOwner()->GetOwner()->GetModel()->GetValue( value, col, row ); + + m_editorCtrl = CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect, value ); + + wxDataViewEditorCtrlEvtHandler *handler = + new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this ); + + m_editorCtrl->PushEventHandler( handler ); + +#if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL) + handler->SetFocusOnIdle(); +#else + m_editorCtrl->SetFocus(); +#endif + + return true; +} + +void wxDataViewRendererBase::CancelEditing() +{ + wxPendingDelete.Append( m_editorCtrl ); + + GetOwner()->GetOwner()->GetMainWindow()->SetFocus(); + + // m_editorCtrl->PopEventHandler( true ); +} + +bool wxDataViewRendererBase::FinishEditing() +{ + wxVariant value; + GetValueFromEditorCtrl( m_editorCtrl, value ); + + wxPendingDelete.Append( m_editorCtrl ); + + GetOwner()->GetOwner()->GetMainWindow()->SetFocus(); + + if (!Validate(value)) + return false; + + unsigned int col = GetOwner()->GetModelColumn(); + GetOwner()->GetOwner()->GetModel()->SetValue( value, col, m_row ); + GetOwner()->GetOwner()->GetModel()->ValueChanged( col, m_row ); + + // m_editorCtrl->PopEventHandler( true ); + + return true; +} + +//----------------------------------------------------------------------------- +// wxDataViewEditorCtrlEvtHandler +//----------------------------------------------------------------------------- + +BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler) + EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar) + EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus) + EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle) +END_EVENT_TABLE() + +wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler( + wxControl *editorCtrl, + wxDataViewRenderer *owner ) +{ + m_owner = owner; + m_editorCtrl = editorCtrl; + + m_finished = false; +} + +void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event ) +{ + if (m_focusOnIdle) + { + m_focusOnIdle = false; + if (wxWindow::FindFocus() != m_editorCtrl) + m_editorCtrl->SetFocus(); + } + + event.Skip(); +} + +void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event ) +{ + switch ( event.m_keyCode ) + { + case WXK_RETURN: + m_finished = true; + m_owner->FinishEditing(); + break; + + case WXK_ESCAPE: + m_finished = true; + m_owner->CancelEditing(); + break; + + default: + event.Skip(); + } +} + +void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event ) +{ + if (!m_finished) + { + m_finished = true; + m_owner->FinishEditing(); + } + + event.Skip(); } // --------------------------------------------------------- @@ -693,29 +838,31 @@ wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype, wxD IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject) -wxDataViewColumnBase::wxDataViewColumnBase(const wxString& title, +wxDataViewColumnBase::wxDataViewColumnBase(const wxString& WXUNUSED(title), wxDataViewRenderer *renderer, unsigned int model_column, int WXUNUSED(width), - int flags ) + wxAlignment WXUNUSED(align), + int WXUNUSED(flags)) { m_renderer = renderer; m_model_column = model_column; - m_flags = flags; - m_title = title; m_owner = NULL; m_renderer->SetOwner( (wxDataViewColumn*) this ); + + // NOTE: the wxDataViewColumn's ctor must store the width, align, flags + // parameters inside the native control! } wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap& bitmap, wxDataViewRenderer *renderer, unsigned int model_column, int WXUNUSED(width), - int flags ) + wxAlignment WXUNUSED(align), + int WXUNUSED(flags) ) { m_renderer = renderer; m_model_column = model_column; - m_flags = flags; m_bitmap = bitmap; m_owner = NULL; m_renderer->SetOwner( (wxDataViewColumn*) this ); @@ -732,26 +879,28 @@ wxDataViewColumnBase::~wxDataViewColumnBase() } } -void wxDataViewColumnBase::SetTitle( const wxString &title ) +int wxDataViewColumnBase::GetFlags() const { - m_title = title; -} + int ret = 0; -wxString wxDataViewColumnBase::GetTitle() -{ - return m_title; -} + if (IsSortable()) + ret |= wxDATAVIEW_COL_SORTABLE; + if (IsResizeable()) + ret |= wxDATAVIEW_COL_RESIZABLE; + if (IsHidden()) + ret |= wxDATAVIEW_COL_HIDDEN; -void wxDataViewColumnBase::SetBitmap( const wxBitmap &bitmap ) -{ - m_bitmap = bitmap; + return ret; } -const wxBitmap &wxDataViewColumnBase::GetBitmap() +void wxDataViewColumnBase::SetFlags(int flags) { - return m_bitmap; + SetSortable((flags & wxDATAVIEW_COL_SORTABLE) != 0); + SetResizeable((flags & wxDATAVIEW_COL_RESIZABLE) != 0); + SetHidden((flags & wxDATAVIEW_COL_HIDDEN) != 0); } + // --------------------------------------------------------- // wxDataViewCtrlBase // --------------------------------------------------------- @@ -766,11 +915,27 @@ wxDataViewCtrlBase::wxDataViewCtrlBase() 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(); + m_model = NULL; + } } bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel *model ) { + if ( m_model ) + m_model->DecRef(); // discard old model, if any + + // add our own reference to the new model: m_model = model; + if ( m_model ) + m_model->IncRef(); return true; } @@ -780,54 +945,84 @@ wxDataViewListModel* wxDataViewCtrlBase::GetModel() return m_model; } -bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewTextRenderer(), model_column ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewTextRenderer( wxT("string"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewToggleRenderer(), model_column, 30 ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewProgressRenderer(), model_column, 70 ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewDateRenderer(), model_column ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewBitmapRenderer(), model_column ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewTextRenderer(), model_column ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewTextRenderer( wxT("string"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewToggleRenderer(), model_column, 30 ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewProgressRenderer(), model_column, 70 ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewDateRenderer(), model_column ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ), + model_column, width, align, flags ) ); } -bool wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column ) +bool wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column, + wxDataViewCellMode mode, int width, wxAlignment align, int flags ) { - return AppendColumn( new wxDataViewColumn( label, new wxDataViewBitmapRenderer(), model_column ) ); + return AppendColumn( new wxDataViewColumn( label, + new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ), + model_column, width, align, flags ) ); } bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col ) @@ -838,7 +1033,7 @@ bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col ) return true; } -unsigned int wxDataViewCtrlBase::GetNumberOfColumns() +unsigned int wxDataViewCtrlBase::GetColumnCount() const { return m_cols.GetCount(); } @@ -866,6 +1061,8 @@ IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent) DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_SELECTED) DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED) +DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK) +DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK) #endif