I have written a following code:
[
Code: Select all
class SortColumnRenderer
#ifndef __WXOSX__
: public wxDataViewCustomRenderer
#else
: public wxDataViewRenderer
#endif
{
public:
static wxString GetDefaultType() { return wxS("wxDataViewCheckIconText"); }
explicit SortColumnRenderer (wxCheckBoxState state = wxCHK_CHECKED, wxDataViewCellMode mode = wxDATAVIEW_CELL_ACTIVATABLE, int align = wxDVR_DEFAULT_ALIGNMENT)
: wxDataViewCustomRenderer(GetDefaultType(), mode, align), m_checkedState(state)
{
m_allow3rdStateForUser = false;
m_value = "Ascending";
}
#ifdef __WXOSX__
virtual bool MacRender() wxOVERRIDE
{
NSButtonCell* cell = (NSButtonCell*) GetNativeData()->GetItemCell();
int nativecbvalue = 0;
switch ( GetCheckedState() )
{
case wxCHK_CHECKED:
nativecbvalue = 1;
break;
case wxCHK_UNDETERMINED:
nativecbvalue = -1;
break;
case wxCHK_UNCHECKED:
nativecbvalue = 0;
break;
}
[cell setIntValue:nativecbvalue];
[cell setTitle:wxCFStringRef( m_value ).AsNSString()];
return true;
}
virtual void OSXOnCellChanged(NSObject *value,const wxDataViewItem& item, unsigned col) wxOVERRIDE
{
wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
// The icon can't be edited so get its old value and reuse it.
wxVariant valueOld;
model->GetValue( valueOld, item, col );
wxDataViewCheckIconText checkIconText;
checkIconText << valueOld;
wxCheckBoxState checkedState ;
switch( ObjectToLong(value) )
{
case 1:
checkedState = wxCHK_CHECKED;
break;
case 0:
checkedState = wxCHK_UNCHECKED;
break;
case -1:
checkedState = m_allow3rdStateForUser ? wxCHK_UNDETERMINED : wxCHK_CHECKED;
break;
}
checkIconText.SetCheckedState( checkedState );
wxVariant valueIconText;
valueIconText << checkIconText;
if( !Validate( valueIconText ) )
return;
model->ChangeValue(valueIconText, item, col);
}
#endif
#ifndef __WXOSX__
virtual bool SetValue(const wxVariant& value) wxOVERRIDE
{
// value = value;
return true;
}
virtual bool GetValue(wxVariant& value) const wxOVERRIDE
{
value = value;
return true;
}
virtual wxSize GetSize() const wxOVERRIDE
{
wxSize size = GetCheckSize();
size.x += MARGIN_CHECK_ICON;
wxString text = _( "Ascending" );
const wxSize sizeText = GetTextExtent( text );
if( sizeText.y > size.y )
size.y = sizeText.y;
size.x += sizeText.x;
return size;
}
virtual bool Render(wxRect cell, wxDC* dc, int state) wxOVERRIDE
{
// Draw the checkbox first.
int renderFlags = 0;
switch( GetCheckedState() )
{
case wxCHK_UNCHECKED:
break;
case wxCHK_CHECKED:
renderFlags |= wxCONTROL_CHECKED;
break;
case wxCHK_UNDETERMINED:
renderFlags |= wxCONTROL_UNDETERMINED;
break;
}
if( state & wxDATAVIEW_CELL_PRELIT )
renderFlags |= wxCONTROL_CURRENT;
const wxSize sizeCheck = GetCheckSize();
wxRect rectCheck( cell.GetPosition(), sizeCheck );
rectCheck = rectCheck.CentreIn( cell, wxVERTICAL );
wxRendererNative::Get().DrawCheckBox( GetView(), *dc, rectCheck, renderFlags );
// Then the icon, if any.
int xoffset = sizeCheck.x + MARGIN_CHECK_ICON;
// Finally the text.
RenderText( _( "Ascending" ), xoffset, cell, dc, state );
return true;
}
virtual bool ActivateCell (const wxRect& cell, wxDataViewModel *model, const wxDataViewItem & item, unsigned int col, const wxMouseEvent *mouseEvent) wxOVERRIDE
{
if( mouseEvent )
{
if( !wxRect( GetCheckSize() ).Contains( mouseEvent->GetPosition() ) )
return false;
}
// If the 3rd state is user-settable then the cycle is
// unchecked->checked->undetermined.
wxCheckBoxState checkedState = GetCheckedState();
switch ( checkedState )
{
case wxCHK_CHECKED:
checkedState = m_allow3rdStateForUser ? wxCHK_UNDETERMINED
: wxCHK_UNCHECKED;
break;
case wxCHK_UNDETERMINED:
// Whether 3rd state is user-settable or not, the next state is
// unchecked.
checkedState = wxCHK_UNCHECKED;
break;
case wxCHK_UNCHECKED:
checkedState = wxCHK_CHECKED;
break;
}
SetCheckedState( checkedState );
wxString value;
value << m_value;
model->ChangeValue( value, item, col );
return true;
}
wxCheckBoxState GetCheckedState() const { return m_checkedState; }
void SetCheckedState(wxCheckBoxState state) { m_checkedState = state; }
#endif
private:
wxSize GetCheckSize () const
{
return wxRendererNative::Get().GetCheckBoxSize(GetView());
}
enum
{
MARGIN_CHECK_ICON = 3
};
bool m_allow3rdStateForUser;
wxCheckBoxState m_checkedState;
wxString m_value;
wxDECLARE_DYNAMIC_CLASS_NO_COPY(SortColumnRenderer);
};
wxIMPLEMENT_CLASS(SortColumnRenderer, wxDataViewRenderer);
and on OSX it doesn't want to compile because NSButtonCell is undefined.06:11:07: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is bool
06:11:07: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is bool
06:11:07: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is bool
06:11:08: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is bool
06:11:08: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is bool
06:11:09: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is bool
06:11:09: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is bool
06:11:10: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is bool
06:11:10: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is string
06:11:10: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is string
06:11:11: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is string
06:11:11: Debug: Wrong type returned from the model for column 1: wxDataViewCheckIconText required but actual type is string
Any suggestions?
Thank you.