Problem with checkbox inside a grid

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
mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Problem with checkbox inside a grid

Post by mbeardsley » Fri Jun 03, 2016 6:39 pm

I am running wxWidgets 3.0.2 on Windows7-64.

One of the columns in my wxGrid is a checkbox that I have implemented using wxGridCellBoolRenderer and wxGridCellBoolEditor.

I then catch the OnGridSelectCell event to look for input. This works fine if the user clicks on the area of the cell which is just to the right of the checkbox (i.e. inside the checkbox cell, but not ON the checkbox itself). However, if the user clicks on the actual checkbox, no event gets generated (even though the checkbox value DOES toggle). The same is true for the OnGridCellLeftClick and OnGridCellChange events - no event gets generated if the checkbox itself is clicked (but events DO get generated if the outer-area is clicked).

By the way, the same behavior can be seen in the "grid" sample. If you click the "pink" area of cell A-4, the selection is displayed in the status area, but if you click the actual checkbox, it seems to be ignored.

Is this the expected behavior?
Is there some other event that I could trap to know when the user has clicked the actual checkbox?

Thanks.

jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: Problem with checkbox inside a grid

Post by jgrzybowski » Fri Jun 03, 2016 7:47 pm

I am not quite sure if I understand correct, but I can advise to use "EditorHidden" event function (in my project it works but without check box, I have wxGridCellChoiceEditor)

Code: Select all

void YourFrameObjectFrm::gridInfoEditorHidden(wxGridEvent& event)
{
	unsigned short int Row = event.GetRow();
	unsigned short int Col = event.GetCol();
	//...
}
Regards, Jarek

mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Re: Problem with checkbox inside a grid

Post by mbeardsley » Fri Jun 03, 2016 8:07 pm

Thanks for the suggestion, but that event doesn't trigger either.

To be more clear, I can click the checkbox (inside the grid cell) on and off without getting any event to trigger, however, if I click the area inside the cell (but outside the actual checkbox), various events will trigger as expected.

I want to be notified when the user clicks anywhere in the cell (inside the checkbox or not).

jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: Problem with checkbox inside a grid

Post by jgrzybowski » Fri Jun 03, 2016 8:42 pm

Take a look on a following solved topic:
viewtopic.php?t=38570
Regards, Jarek

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

Re: Problem with checkbox inside a grid

Post by doublemax » Fri Jun 03, 2016 8:46 pm

First of all: I think this is a bug that should be reported at http://trac.wxwidgets.org/

Second, i think you should receive a wxEVT_CHECKBOX event. Haven't tried it though.
Use the source, Luke!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2117
Joined: Sun Jan 03, 2010 5:45 pm

Re: Problem with checkbox inside a grid

Post by PB » Fri Jun 03, 2016 8:51 pm

If everthing else fails, you can catch the click from the editor control, seems to work but I don't know if this is the correct way:

Code: Select all

#include <wx/wx.h>
#include <wx/grid.h>

class MyFrame : public wxFrame
{
public:
    MyFrame()
        : wxFrame(NULL, wxID_ANY, ("Test"), wxDefaultPosition, wxSize(500,500))
    {                
        m_grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0);        
        m_grid->CreateGrid(10, 2);
        m_grid->SetColFormatBool(boolColIdx);

        m_grid->Bind(wxEVT_GRID_EDITOR_CREATED, &MyFrame::OnEditorCreated, this);
        m_grid->Bind(wxEVT_GRID_CELL_LEFT_CLICK, &MyFrame::OnBoolCellLClick, this);

        wxTextCtrl* textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);
        wxLog::SetActiveTarget(new wxLogTextCtrl(textCtrl));

        wxBoxSizer* sizer;
        sizer = new wxBoxSizer(wxVERTICAL);
        sizer->Add(m_grid, 4, wxALL | wxEXPAND, 5);
        sizer->Add(textCtrl, 1, wxALL | wxEXPAND, 5);        
        SetSizer(sizer);
    }
private:        
    enum
    {
        stringColIdx = 0,
        boolColIdx = 1,
    };

    wxGrid* m_grid;

    void OnEditorCreated(wxGridEditorCreatedEvent& e)
    {
        if ( e.GetCol() == boolColIdx )
        {
            wxLogMessage("Bool editor created.");
            wxControl* control = e.GetControl();
            if ( control )
                control->Bind(wxEVT_LEFT_DOWN, &MyFrame::OnBoolEditorClicked, this);
        }
    }

    void OnBoolCellLClick(wxGridEvent& e)
    {
        if ( e.GetCol() == boolColIdx )        
            wxLogMessage("Bool cell clicked.");                    
        e.Skip();
    }

    void OnBoolEditorClicked(wxMouseEvent& e)
    {
        wxLogMessage("Bool editor clicked.");
        e.Skip();
    }
};

class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {
        (new MyFrame)->Show();
        return true;
    }
};
wxIMPLEMENT_APP(MyApp);

mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Re: Problem with checkbox inside a grid

Post by mbeardsley » Fri Jun 03, 2016 9:02 pm

If anything else fails, catch the click from the editor control
No, that doesn't work. Clicking the checkbox on/off doesn't cause the event to trigger.
I think you should receive a wxEVT_CHECKBOX event.
That's an interesting idea, but how would I know which row/column in the grid was being clicked?
Moreover, how would I even bind to that event from a wxGrid control?
Remember that it's not really a checkbox control, it's a wxGridCellBoolEditor/wxGridCellBoolRenderer.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2117
Joined: Sun Jan 03, 2010 5:45 pm

Re: Problem with checkbox inside a grid

Post by PB » Fri Jun 03, 2016 9:06 pm

mbeardsley wrote:
If anything else fails, catch the click from the editor control
No, that doesn't work. Clicking the checkbox on/off doesn't cause the event to trigger.
Odd, it does work for me, i.e. the events I'm catching there are fired (see the GIF below). have you tried the exact code I posted?
Animation.gif
Animation.gif (320.56 KiB) Viewed 1608 times
Last edited by PB on Fri Jun 03, 2016 9:16 pm, edited 1 time in total.

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

Re: Problem with checkbox inside a grid

Post by doublemax » Fri Jun 03, 2016 9:11 pm

That's an interesting idea, but how would I know which row/column in the grid was being clicked?
Current cursor position?
Moreover, how would I even bind to that event from a wxGrid control?
It's a "normal" wxCommandEvent which propagate upwards in the window hierarchy. Catch it the same way you would for a wxCheckbox.
Remember that it's not really a checkbox control, it's a wxGridCellBoolEditor/wxGridCellBoolRenderer.
The editor contains a "real" wxCheckbox.
Use the source, Luke!

mbeardsley
Knows some wx things
Knows some wx things
Posts: 46
Joined: Thu Sep 25, 2014 7:40 pm

Re: Problem with checkbox inside a grid

Post by mbeardsley » Tue Jun 07, 2016 3:59 pm

I tried various ways to work around the problem, (mostly trying to interface to the underlying checkbox) all of which seemed to have their own issues.

However, I found a much simpler way of doing what I want.

Rather than using a wxGridCellBoolEditor AND a wxGridCellBoolRenderer, I am now just using the wxGridCellBoolRenderer and the "normal" cell editor. This way, when any part of the cell is clicked, I get a cell selection event, but the cell is still rendered as a checkbox.

In my event handler, I simply toggle the cell value between "1" and "", and it seems to work fine.

Thanks for all of the replies.

Post Reply