Renderer for wxDVLC: *nix and Mac

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4255
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Renderer for wxDVLC: *nix and Mac

Post by ONEEYEMAN » Mon Jan 27, 2020 1:45 pm

Hi,
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);

which gives a runtime GTK warning like:
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
and on OSX it doesn't want to compile because NSButtonCell is undefined.

Any suggestions?

Thank you.

Post Reply