Added an example of overriding method HasValue in wxDataView to show how some items may not have values for some columns.

This commit is contained in:
Jorge Moraleda 2020-04-13 23:43:39 -07:00
parent a1f90ae0de
commit 8b9cf87653
3 changed files with 77 additions and 0 deletions

View File

@ -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<wxVariant> 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;
}
}

View File

@ -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;
}

View File

@ -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
// ----------------------------------------------------------------------------