Using Col_EditableText, Col_IconText and Col_TextWithAttr instead of 0, 1 and
2 makes the sample code a bit easier to read.
Also use switch on the column value instead of nested ifs everywhere to give
compiler a chance to warn us if we forget to update some function when a new
column is added.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62587
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
m_ctrl[1]->AssociateModel( m_list_model.get() );
// the various columns
m_ctrl[1]->AssociateModel( m_list_model.get() );
// the various columns
- m_ctrl[1]->AppendTextColumn("editable string", 0, wxDATAVIEW_CELL_EDITABLE);
- m_ctrl[1]->AppendIconTextColumn("icon", 1, wxDATAVIEW_CELL_EDITABLE);
+ m_ctrl[1]->AppendTextColumn("editable string",
+ MyListModel::Col_EditableText,
+ wxDATAVIEW_CELL_EDITABLE);
+ m_ctrl[1]->AppendIconTextColumn("icon",
+ MyListModel::Col_IconText,
+ wxDATAVIEW_CELL_EDITABLE);
- new wxDataViewColumn("attributes", new wxDataViewTextRenderer, 2 ));
+ new wxDataViewColumn("attributes",
+ new wxDataViewTextRenderer,
+ MyListModel::Col_TextWithAttr)
+ );
void MyListModel::GetValueByRow( wxVariant &variant,
unsigned int row, unsigned int col ) const
{
void MyListModel::GetValueByRow( wxVariant &variant,
unsigned int row, unsigned int col ) const
{
- if (col==0)
- {
- if (row >= m_textColValues.GetCount())
- variant = wxString::Format( "virtual row %d", row );
- else
- variant = m_textColValues[ row ];
- }
- else if (col==1)
- wxString text;
- if ( row >= m_iconColValues.GetCount() )
- text = "virtual icon";
- else
- text = m_iconColValues[row];
+ case Col_EditableText:
+ if (row >= m_textColValues.GetCount())
+ variant = wxString::Format( "virtual row %d", row );
+ else
+ variant = m_textColValues[ row ];
+ break;
- variant << wxDataViewIconText(text, m_icon[row % 2]);
- }
- else if (col==2)
- {
- static const char *labels[5] =
- {
- "blue", "green", "red", "bold cyan", "default",
- };
+ case Col_IconText:
+ {
+ wxString text;
+ if ( row >= m_iconColValues.GetCount() )
+ text = "virtual icon";
+ else
+ text = m_iconColValues[row];
+
+ variant << wxDataViewIconText(text, m_icon[row % 2]);
+ }
+ break;
+
+ case Col_TextWithAttr:
+ {
+ static const char *labels[5] =
+ {
+ "blue", "green", "red", "bold cyan", "default",
+ };
+
+ variant = labels[row % 5];
+ }
+ break;
- variant = labels[row % 5];
+ case Col_Max:
+ wxFAIL_MSG( "invalid column" );
if ( !(row % 2) )
return false;
attr.SetColour(*wxLIGHT_GREY);
break;
if ( !(row % 2) )
return false;
attr.SetColour(*wxLIGHT_GREY);
break;
// do what the labels defined above hint at
switch ( row % 5 )
{
// do what the labels defined above hint at
switch ( row % 5 )
{
+
+ case Col_Max:
+ wxFAIL_MSG( "invalid column" );
+ case Col_EditableText:
+ case Col_IconText:
if (row >= m_textColValues.GetCount())
{
// the item is not in the range of the items
if (row >= m_textColValues.GetCount())
{
// the item is not in the range of the items
+ if ( col == Col_EditableText )
{
m_textColValues[row] = variant.GetString();
}
{
m_textColValues[row] = variant.GetString();
}
+ else // col == Col_IconText
{
wxDataViewIconText iconText;
iconText << variant;
m_iconColValues[row] = iconText.GetText();
}
{
wxDataViewIconText iconText;
iconText << variant;
m_iconColValues[row] = iconText.GetText();
}
wxLogError("Cannot edit the column %d", col);
wxLogError("Cannot edit the column %d", col);
+ break;
+
+ case Col_Max:
+ wxFAIL_MSG( "invalid column" );
class MyListModel: public wxDataViewVirtualListModel
{
public:
class MyListModel: public wxDataViewVirtualListModel
{
public:
+ enum
+ {
+ Col_EditableText,
+ Col_IconText,
+ Col_TextWithAttr,
+ Col_Max
+ };
+
MyListModel();
// helper methods to change the model
MyListModel();
// helper methods to change the model
virtual unsigned int GetColumnCount() const
{
virtual unsigned int GetColumnCount() const
{
}
virtual wxString GetColumnType( unsigned int col ) const
{
}
virtual wxString GetColumnType( unsigned int col ) const
{
+ if (col == Col_IconText)
return wxT("wxDataViewIconText");
return wxT("string");
return wxT("wxDataViewIconText");
return wxT("string");