how to embed button in wxDataViewListCtrl? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
chenbin.sh
Experienced Solver
Experienced Solver
Posts: 64
Joined: Fri Apr 17, 2009 7:15 am
Location: Sydney, Australia
Contact:

how to embed button in wxDataViewListCtrl?

Post by chenbin.sh » Thu Apr 10, 2014 1:31 pm

could somebody show me the sample code?

UPDATE 1:
I find some workaround, it's kind of dirty. if you check the wxDataViewListCtrl documentation, you will find that it cannot detect mouse left button down event, especially for GTK.

So I just do everything in the custom button render

Code: Select all

#include <wx/dataview.h>
#include <wx/timer.h>

class wxDataViewMyButtonRenderer: public wxDataViewCustomRenderer, public wxTimer
{
public:
    wxDataViewMyButtonRenderer( const wxString &varianttype = wxT("wxString"),
                              wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
                              int align = wxDVR_DEFAULT_ALIGNMENT );

    virtual bool SetValue( const wxVariant &value );
    virtual bool GetValue( wxVariant &value ) const;

    virtual bool Render( wxRect, wxDC*, int);
    virtual wxSize GetSize() const;
    virtual void Notify();
    void SetParent(wxWindow*);
     // Implementation only, don't use nor override
    virtual bool ActivateCell(const wxRect& rect,
        wxDataViewModel *model,
        const wxDataViewItem& item,
        unsigned int col,
        const wxMouseEvent *mouseEvent);

private:
    wxString m_value;
    bool m_button_clicked;
    wxRect m_button_rect;
    wxDC* m_button_dc;
    wxWindow* m_parent;
    // wxTimer m_button_timer;

protected:
    DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewMyButtonRenderer)
};
When user click one row, the ActivateCell is called, we can use some rect detect algorithm to find if the button rect is clicked.

There is no mouse up event, so I have to hack, basically start a wxTimer when button clicked and draw the button up effect after about 1 second in Notify()

To draw the button up effect, I need ask the parent data view list to refresh itself, so that the button's Render() method has a chance to be called. That's why we need SetParent().

If anyone has better idea, please enlighten me.

UPDATE 2:
I added more tech details at my blog:
http://blog.binchen.org/?p=1118
Last edited by chenbin.sh on Sun Apr 13, 2014 1:39 pm, edited 5 times in total.
help me, help you.
http://blog.binchen.org

User avatar
doublemax
Moderator
Moderator
Posts: 15655
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: how to embed button in wxDataViewListCtrl?

Post by doublemax » Thu Apr 10, 2014 7:40 pm

You can't put an actual wxButton in a wxDataViewListCtrl. You have to create a custom renderer that draws a button. Look into <wxdir>/src/generic/datavgen.cpp and check how the wxDataViewToggleRenderer is implemented. You should be able to do something similar for a button.
Use the source, Luke!

Post Reply