Sam
Sam

Reputation: 796

How do I handle the wxEVT_GRID_COL_SORT event to sort a grid?

I'm trying to sort a wxGrid. Now, the documentation tells me it doesn't support sorting, but that it does generate an event. This documentation tells me the event is called wxEVT_GRID_COL_SORT. Fair enough!

Now, the problem is that I simply don't know how to get the event to work. I have an event table for my frame, which looks something like this:

BEGIN_EVENT_TABLE(MainWindow, wxFrame)
EVT_BUTTON(XRCID("toevoegknop"), MainWindow::openAddWindow)
// A few other events that work
END_EVENT_TABLE()

The events listed there already work perfectly fine. In my MainWindow class I declared a function:

void sortColumn(wxGridEvent& event);

Now, I want to add said wxEVT_GRID_COL_SORT event. From my point of view the documentation isn't really super clear on what I should do, so I just tried adding the event by adding the following line to the event table.

wxEVT_GRID_COL_SORT(MainWindow::sortColumn)

Brings up a syntax error, thus it isn't right. I noticed the other events just started with EVT, so I tried to remove the wx, but I'm still out of luck.

Searching the internet far and wide brought me to a pastebin post which handles the event by adding the following line to the constructor of the frame (in my case MainWindow)

Grid->Connect(wxEVT_GRID_COL_SORT,(wxObjectEventFunction)&Frame::OnGridColSort);

Which I adapted like this (the almost entire constructor for MainWindow)

MainWindow::MainWindow(const wxString& title, const wxPoint& pos, const wxSize& size, Collection* library, MovieDB* database)
: wxFrame(), library_(library), database_(database) {
wxXmlResource::Get()->LoadFrame(this, NULL, _T("hoofdvenster"));

SetSize(size);
grid_ = (wxGrid *)FindWindowById(XRCID("filmtabel"));
// Irrelevant code removed, setting up the grid labels etc.

grid_->Connect(wxEVT_GRID_COL_SORT,(wxObjectEventFunction)&MainWindow::sortColumn);
}

Which throws up an error:

‘wxEVT_GRID_COL_SORT’ was not declared in this scope

So now I don't know what else I can try. Please do keep in mind that I only started messing about with wxWidgets a few days ago, so things that are trivial to any wxWidgets user might not be for me.

Thanks in advance!

Upvotes: 2

Views: 1188

Answers (1)

Anonymous Coward
Anonymous Coward

Reputation: 6226

It appears that you're using wx 2.8.12 which does not implement wxEVT_GRID_COL_SORT. It was added in wx 2.9 so you'd have to get the latest development release (2.9.4) to use it.

However, in wx 2.8 you can handle wxEVT_GRID_LABEL_LEFT_CLICK and dispatch the event accordingly to emulate the event.

Either add the event to the event map,

EVT_GRID_CMD_LABEL_LEFT_CLICK(ID_GRID,Frame::OnGridLabelLeftClick)

or connect it in your constructor:

grid->Connect(wxEVT_GRID_LABEL_LEFT_CLICK,
    (wxObjectEventFunction)&Frame::OnGridLabelLeftClick);
void Frame::OnGridColSort(wxGridEvent& event) {}
void Frame::OnGridRowSort(wxGridEvent& event) {}

void Frame::OnGridLabelLeftClick(wxGridEvent& event) {
    // GetCol and GetRow will return the index of the col/row label clicked
    event.Skip(); // the next handler will select col/row/everything, based
                  // on the label clicked; remove to prevent selection
    if( event.GetCol() >= 0 )
        OnGridColSort(event);
    else if( event.GetRow() >= 0 )
        OnGridRowSort(event);
    else
        ; // if both are -1, the upper left corner was clicked (select all)
}

This will behave similarly to EVT_GRID_COL_SORT.

Upvotes: 3

Related Questions