From 8b9cf87653077d359aa7237996f8032148567c5e Mon Sep 17 00:00:00 2001 From: Jorge Moraleda Date: Mon, 13 Apr 2020 23:43:39 -0700 Subject: [PATCH] Added an example of overriding method HasValue in wxDataView to show how some items may not have values for some columns. --- samples/dataview/dataview.cpp | 55 +++++++++++++++++++++++++++++++++++ samples/dataview/mymodels.cpp | 12 ++++++++ samples/dataview/mymodels.h | 10 +++++++ 3 files changed, 77 insertions(+) diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index e7a00ab617..f6cab09aa8 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -182,6 +182,7 @@ private: Page_TreeStore, Page_VarHeight, Page_IndexList, + Page_HasValue, Page_Max }; @@ -713,6 +714,16 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int sixthPanelSz->Add(button_sizer6); sixthPanel->SetSizerAndFit(sixthPanelSz); + // page showing that some columns don't have values for some items + // --------------------------------------------------------------- + + wxPanel *seventhPanel = new wxPanel( m_notebook, wxID_ANY ); + + BuildDataViewCtrl(seventhPanel, Page_HasValue); + + wxSizer *seventhPanelSz = new wxBoxSizer( wxVERTICAL ); + seventhPanelSz->Add(m_ctrl[Page_HasValue], 1, wxGROW|wxALL, 5); + seventhPanel->SetSizerAndFit(seventhPanelSz); // complete GUI // ------------ @@ -723,6 +734,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int m_notebook->AddPage(fourthPanel, "wxDataViewTreeCtrl"); m_notebook->AddPage(fifthPanel, "Variable line height"); m_notebook->AddPage(sixthPanel, "MyIndexListModel"); + m_notebook->AddPage(seventhPanel, "MyDataViewHasValue"); wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL); @@ -987,7 +999,50 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l this); } break; + + case Page_HasValue: + { + wxDataViewListCtrl* lc = + new wxDataViewListCtrl( parent, wxID_ANY, wxDefaultPosition, + wxDefaultSize, style ); + m_ctrl[Page_HasValue] = lc; + + MyListStoreDerivedModel* page7_model = new MyListStoreHasValueModel(); + lc->AssociateModel(page7_model); + page7_model->DecRef(); + + lc->AppendToggleColumn( "Toggle" ); + + // We're not limited to convenience column-appending functions, it + // can also be done fully manually, which allows us to customize + // the renderer being used. + wxDataViewToggleRenderer* const rendererRadio = + new wxDataViewToggleRenderer("bool", wxDATAVIEW_CELL_ACTIVATABLE); + rendererRadio->ShowAsRadio(); + wxDataViewColumn* const colRadio = + new wxDataViewColumn("Radio", rendererRadio, 1); + lc->AppendColumn(colRadio, "bool"); + + lc->AppendTextColumn( "Text" ); + lc->AppendProgressColumn( "Progress" )->SetMinWidth(FromDIP(100)); + + wxVector data; + for (unsigned int i=0; i<10; i++) + { + data.clear(); + data.push_back( (i%3) == 0 ); + data.push_back( i == 7 ); // select a single (random) radio item + data.push_back( wxString::Format("row %d", i) ); + data.push_back( long(5*i) ); + + lc->AppendItem( data ); + } + + lc->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &MyFrame::OnListValueChanged, this); + } + break; } + } diff --git a/samples/dataview/mymodels.cpp b/samples/dataview/mymodels.cpp index 9c5ee3112b..fc60e8bb4f 100644 --- a/samples/dataview/mymodels.cpp +++ b/samples/dataview/mymodels.cpp @@ -626,3 +626,15 @@ bool MyListStoreDerivedModel::IsEnabledByRow(unsigned int row, unsigned int col) // disabled the last two checkboxes return !(col == 0 && 8 <= row && row <= 9); } + +// ---------------------------------------------------------------------------- +// MyListStoreHasValueModel +// ---------------------------------------------------------------------------- + +bool MyListStoreHasValueModel::HasValue(const wxDataViewItem &item, unsigned int col) const +{ + unsigned int row = GetRow( item ); + // the diagonal entries don't have values. This is just a silly example to demonstrate the + // usage of overriding HasValue to specify that some columns don't have values for some items + return row != col; +} diff --git a/samples/dataview/mymodels.h b/samples/dataview/mymodels.h index 8cab532f1f..4fbeb23e2e 100644 --- a/samples/dataview/mymodels.h +++ b/samples/dataview/mymodels.h @@ -266,6 +266,16 @@ public: virtual bool IsEnabledByRow(unsigned int row, unsigned int col) const wxOVERRIDE; }; +// ---------------------------------------------------------------------------- +// MyListStoreHasValueModel +// ---------------------------------------------------------------------------- + +class MyListStoreHasValueModel : public MyListStoreDerivedModel +{ +public: + virtual bool HasValue(const wxDataViewItem &item, unsigned int col) const wxOVERRIDE; +}; + // ---------------------------------------------------------------------------- // MyIndexListModel // ----------------------------------------------------------------------------