Use symbolic constants for list model columns in dataview sample.
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
This commit is contained in:
@@ -545,10 +545,17 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l
|
|||||||
m_ctrl[1]->AssociateModel( m_list_model.get() );
|
m_ctrl[1]->AssociateModel( m_list_model.get() );
|
||||||
|
|
||||||
// the various columns
|
// the various columns
|
||||||
m_ctrl[1]->AppendTextColumn("editable string", 0, wxDATAVIEW_CELL_EDITABLE);
|
m_ctrl[1]->AppendTextColumn("editable string",
|
||||||
m_ctrl[1]->AppendIconTextColumn("icon", 1, wxDATAVIEW_CELL_EDITABLE);
|
MyListModel::Col_EditableText,
|
||||||
|
wxDATAVIEW_CELL_EDITABLE);
|
||||||
|
m_ctrl[1]->AppendIconTextColumn("icon",
|
||||||
|
MyListModel::Col_IconText,
|
||||||
|
wxDATAVIEW_CELL_EDITABLE);
|
||||||
m_ctrl[1]->AppendColumn(
|
m_ctrl[1]->AppendColumn(
|
||||||
new wxDataViewColumn("attributes", new wxDataViewTextRenderer, 2 ));
|
new wxDataViewColumn("attributes",
|
||||||
|
new wxDataViewTextRenderer,
|
||||||
|
MyListModel::Col_TextWithAttr)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@@ -404,31 +404,40 @@ void MyListModel::AddMany()
|
|||||||
void MyListModel::GetValueByRow( wxVariant &variant,
|
void MyListModel::GetValueByRow( wxVariant &variant,
|
||||||
unsigned int row, unsigned int col ) const
|
unsigned int row, unsigned int col ) const
|
||||||
{
|
{
|
||||||
if (col==0)
|
switch ( col )
|
||||||
{
|
{
|
||||||
if (row >= m_textColValues.GetCount())
|
case Col_EditableText:
|
||||||
variant = wxString::Format( "virtual row %d", row );
|
if (row >= m_textColValues.GetCount())
|
||||||
else
|
variant = wxString::Format( "virtual row %d", row );
|
||||||
variant = m_textColValues[ row ];
|
else
|
||||||
}
|
variant = m_textColValues[ row ];
|
||||||
else if (col==1)
|
break;
|
||||||
{
|
|
||||||
wxString text;
|
|
||||||
if ( row >= m_iconColValues.GetCount() )
|
|
||||||
text = "virtual icon";
|
|
||||||
else
|
|
||||||
text = m_iconColValues[row];
|
|
||||||
|
|
||||||
variant << wxDataViewIconText(text, m_icon[row % 2]);
|
case Col_IconText:
|
||||||
}
|
{
|
||||||
else if (col==2)
|
wxString text;
|
||||||
{
|
if ( row >= m_iconColValues.GetCount() )
|
||||||
static const char *labels[5] =
|
text = "virtual icon";
|
||||||
{
|
else
|
||||||
"blue", "green", "red", "bold cyan", "default",
|
text = m_iconColValues[row];
|
||||||
};
|
|
||||||
|
|
||||||
variant = labels[row % 5];
|
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;
|
||||||
|
|
||||||
|
case Col_Max:
|
||||||
|
wxFAIL_MSG( "invalid column" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -437,16 +446,16 @@ bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col,
|
|||||||
{
|
{
|
||||||
switch ( col )
|
switch ( col )
|
||||||
{
|
{
|
||||||
case 0:
|
case Col_EditableText:
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case 1:
|
case Col_IconText:
|
||||||
if ( !(row % 2) )
|
if ( !(row % 2) )
|
||||||
return false;
|
return false;
|
||||||
attr.SetColour(*wxLIGHT_GREY);
|
attr.SetColour(*wxLIGHT_GREY);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case Col_TextWithAttr:
|
||||||
// do what the labels defined above hint at
|
// do what the labels defined above hint at
|
||||||
switch ( row % 5 )
|
switch ( row % 5 )
|
||||||
{
|
{
|
||||||
@@ -471,6 +480,9 @@ bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case Col_Max:
|
||||||
|
wxFAIL_MSG( "invalid column" );
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -481,8 +493,8 @@ bool MyListModel::SetValueByRow( const wxVariant &variant,
|
|||||||
{
|
{
|
||||||
switch ( col )
|
switch ( col )
|
||||||
{
|
{
|
||||||
case 0:
|
case Col_EditableText:
|
||||||
case 1:
|
case Col_IconText:
|
||||||
if (row >= m_textColValues.GetCount())
|
if (row >= m_textColValues.GetCount())
|
||||||
{
|
{
|
||||||
// the item is not in the range of the items
|
// the item is not in the range of the items
|
||||||
@@ -492,22 +504,25 @@ bool MyListModel::SetValueByRow( const wxVariant &variant,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( col == 0 )
|
if ( col == Col_EditableText )
|
||||||
{
|
{
|
||||||
m_textColValues[row] = variant.GetString();
|
m_textColValues[row] = variant.GetString();
|
||||||
}
|
}
|
||||||
else // col == 1
|
else // col == Col_IconText
|
||||||
{
|
{
|
||||||
wxDataViewIconText iconText;
|
wxDataViewIconText iconText;
|
||||||
iconText << variant;
|
iconText << variant;
|
||||||
m_iconColValues[row] = iconText.GetText();
|
m_iconColValues[row] = iconText.GetText();
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case Col_TextWithAttr:
|
||||||
|
wxLogError("Cannot edit the column %d", col);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
case Col_Max:
|
||||||
wxLogError("Cannot edit the column %d", col);
|
wxFAIL_MSG( "invalid column" );
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -188,6 +188,14 @@ private:
|
|||||||
class MyListModel: public wxDataViewVirtualListModel
|
class MyListModel: public wxDataViewVirtualListModel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
Col_EditableText,
|
||||||
|
Col_IconText,
|
||||||
|
Col_TextWithAttr,
|
||||||
|
Col_Max
|
||||||
|
};
|
||||||
|
|
||||||
MyListModel();
|
MyListModel();
|
||||||
|
|
||||||
// helper methods to change the model
|
// helper methods to change the model
|
||||||
@@ -202,12 +210,12 @@ public:
|
|||||||
|
|
||||||
virtual unsigned int GetColumnCount() const
|
virtual unsigned int GetColumnCount() const
|
||||||
{
|
{
|
||||||
return 3;
|
return Col_Max;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual wxString GetColumnType( unsigned int col ) const
|
virtual wxString GetColumnType( unsigned int col ) const
|
||||||
{
|
{
|
||||||
if (col == 1)
|
if (col == Col_IconText)
|
||||||
return wxT("wxDataViewIconText");
|
return wxT("wxDataViewIconText");
|
||||||
|
|
||||||
return wxT("string");
|
return wxT("string");
|
||||||
|
Reference in New Issue
Block a user