Page 1 of 1

wxGrid and validator

Posted: Thu Mar 05, 2009 10:33 am
by MuhammadSohail
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?

Posted: Thu Mar 05, 2009 12:48 pm
by JimFairway
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

Posted: Thu Mar 05, 2009 2:10 pm
by MuhammadSohail
i have wxWidget_v2.8.8

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

Posted: Thu Mar 05, 2009 3:14 pm
by JimFairway
I discovered them by looking at the source code a while back.
(src/generic/grid.cpp)

Jim

Posted: Thu Mar 05, 2009 3:36 pm
by MuhammadSohail
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);


Posted: Thu Mar 05, 2009 3:45 pm
by MuhammadSohail
For your ino: i want to write in the selected cell
00000 - FFFFF

Posted: Thu Mar 05, 2009 6:36 pm
by JimFairway
You can build that logic into your IsAcceptedKey routine too.

Posted: Mon Mar 09, 2009 10:08 am
by MuhammadSohail
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??

Posted: Mon Mar 09, 2009 1:46 pm
by JimFairway
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

Posted: Tue Mar 10, 2009 11:39 am
by MuhammadSohail
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().

Posted: Wed Mar 11, 2009 12:22 pm
by JimFairway
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

Posted: Thu Jun 04, 2009 9:26 am
by MuhammadSohail
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); 

}