wxGrid and validator 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
MuhammadSohail
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Jun 17, 2005 1:53 pm
Location: Germany
Contact:

wxGrid and validator

Post by MuhammadSohail » Thu Mar 05, 2009 10:33 am

Is there any method or way to create wxGrid with validator on each cell.

At the mement, wxGrid provides wxGridCellTextEditor, wxGriddCellNumberEditor,... for each cell.

How do i set the hex nuumber validor on each cell of the Grid?

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Thu Mar 05, 2009 12:48 pm

Hi,

One choice is to derive a class from wxGridCellNumberEditor and override the IsAcceptedKey(wxKeyEvent& event) and StartingKey(wxKeyEvent& event) methods .
Here is a modification from the wxGridCellNumberEditor source code :

Code: Select all

bool myHexNumberEditor::IsAcceptedKey(wxKeyEvent& event)
{
    if ( wxGridCellEditor::IsAcceptedKey(event) )
    {
        int keycode = event.GetKeyCode();
        if ( (keycode < 128) &&
             (wxIsxdigit(keycode) || keycode == '+' || keycode == '-'))
        {
            return true;
        }
    }

    return false;
}

void myHexNumberEditor::StartingKey(wxKeyEvent& event)
{
    int keycode = event.GetKeyCode();
    if ((keycode < 128) && ( wxIsxdigit(keycode) || keycode == '+' || keycode == '-'))
        {
            wxGridCellTextEditor::StartingKey(event);

            // skip Skip() below
            return;
        }
    }
    event.Skip();
}
Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

MuhammadSohail
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Jun 17, 2005 1:53 pm
Location: Germany
Contact:

Post by MuhammadSohail » Thu Mar 05, 2009 2:10 pm

i have wxWidget_v2.8.8

I could not find these methods in the documentation
wxGridCellEditor::IsAcceptedKey(event)
wxIsxdigit(keycode)

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Thu Mar 05, 2009 3:14 pm

I discovered them by looking at the source code a while back.
(src/generic/grid.cpp)

Jim
OS: Vista SP1, wxWidgets 2.8.7.

MuhammadSohail
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Jun 17, 2005 1:53 pm
Location: Germany
Contact:

Post by MuhammadSohail » Thu Mar 05, 2009 3:36 pm

I have my own HexNumberEditor implemented as you said, but unfortunately it only works if the cell is currently selected and then key is pressed.

If i double click on the cell and starts writing again, editor doesnot call the overloaded functions. why??

Code: Select all

class HexNumberEditor : public wxGridCellTextEditor
{
public:
	bool IsAcceptedKey(wxKeyEvent& event);
	void StartingKey(wxKeyEvent& event);
};


bool HexNumberEditor::IsAcceptedKey(wxKeyEvent& event)
{
    if ( wxGridCellEditor::IsAcceptedKey(event) )
    {
        int keycode = event.GetKeyCode();
        if ( (keycode < 128) && wxIsxdigit(keycode) )
        {
            return true;
        }
    }

    return false;
}

void HexNumberEditor::StartingKey(wxKeyEvent& event)
{
    int keycode = event.GetKeyCode();
    if ((keycode < 128) && wxIsxdigit(keycode) )
    {
        wxGridCellTextEditor::StartingKey(event);

        // skip Skip() below
        return;
    }
    event.Skip();
}

HexNumberGridEditor* editor = new HexNumberGridEditor();
				
editor->SetParameters("6");
 
d_myGridCtrl->SetCellEditor(row, col, editor);


MuhammadSohail
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Jun 17, 2005 1:53 pm
Location: Germany
Contact:

Post by MuhammadSohail » Thu Mar 05, 2009 3:45 pm

For your ino: i want to write in the selected cell
00000 - FFFFF

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Thu Mar 05, 2009 6:36 pm

You can build that logic into your IsAcceptedKey routine too.
OS: Vista SP1, wxWidgets 2.8.7.

MuhammadSohail
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Jun 17, 2005 1:53 pm
Location: Germany
Contact:

Post by MuhammadSohail » Mon Mar 09, 2009 10:08 am

My question is the same, the callback function only called if the cell is currently selected. If i double click on the cell and starts writing , this function doesnot invoked. Why??

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Mon Mar 09, 2009 1:46 pm

MuhammadSohail wrote:My question is the same, the callback function only called if the cell is currently selected. If i double click on the cell and starts writing , this function doesnot invoked. Why??
Not sure what you mean by the "is the same", what I suggested works fine for me. Perhaps you should post some code.

Jim
OS: Vista SP1, wxWidgets 2.8.7.

MuhammadSohail
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Jun 17, 2005 1:53 pm
Location: Germany
Contact:

Post by MuhammadSohail » Tue Mar 10, 2009 11:39 am

The callback function only called if the cell is currently selected.

e.g.
---------------

---------------

If i double click on the cell and starts writing ,

---------------
0fg
---------------

Here 'g' is not allowed but the callback functions are not invoked to validate this character. Why??

My code is already attached in the previous post().

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Wed Mar 11, 2009 12:22 pm

Hi,

I guess I should have looked at this a bit more, I can replicate the behavior you're talking about.

There are a couple of solutions, both of which require that you install a wxValidator for the wxTextControl used for the cell.
You can get access to the wxTextControl using this->Text() inside of the HexNumberEditor object.
I.e.
You need to define a validator that only allows hex characters, and assign it to that object (see wxTextValidator for an example. http://docs.wxwidgets.org/stable/wx_wxt ... dator.html ).

Code: Select all

this->Text()->SetValidator(new myHexValidator);
The challenge is that the wxTextCtrl object does not exist until your edit control has been created, which happens the first time the cell is selected by the user.
So you either have to catch the EVT_GRID_CMD_EDITOR_CREATED event, which is raised after the wxTextCtrl is created, at which point you can assign the validator to it.
Or you can override the Create method for your HexNumberEditor
as follows:

Code: Select all

void HexNumberEditor::Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler)
{
	wxGridCellTextEditor::Create(parent,id, evtHandler);
	this->Text()->SetValidator(new myHexValidator);
}
Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

MuhammadSohail
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Jun 17, 2005 1:53 pm
Location: Germany
Contact:

Post by MuhammadSohail » Thu Jun 04, 2009 9:26 am

Thanks a lot Jim Fairy, it works
HexNumberGridEditor class overides the Create() functiion and includes the following code

Code: Select all

HexNumberGridEditor::Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler)
{
wxArrayString includeList; 
includeList.Add(wxT("0")); 
includeList.Add(wxT("1")); 
includeList.Add(wxT("2")); 
includeList.Add(wxT("3")); 
includeList.Add(wxT("4")); 
includeList.Add(wxT("5")); 
includeList.Add(wxT("6")); 
includeList.Add(wxT("7")); 
includeList.Add(wxT("8")); 
includeList.Add(wxT("9")); 
includeList.Add(wxT("a")); 
includeList.Add(wxT("b")); 
includeList.Add(wxT("c")); 
includeList.Add(wxT("d")); 
includeList.Add(wxT("e"));
includeList.Add(wxT("f"));
includeList.Add(wxT("A"));
includeList.Add(wxT("B"));
includeList.Add(wxT("C"));
includeList.Add(wxT("D"));
includeList.Add(wxT("E"));
includeList.Add(wxT("F"));

wxTextValidator textValidator(wxFILTER_INCLUDE_CHAR_LIST); 
textValidator.SetIncludes(includeList); 

wxGridCellTextEditor::Create(parent,id, evtHandler);
this->Text()->SetValidator( textValidator); 

}

Post Reply