I'm currently writing a csv visualization task using wxWidgets, and I use notebook to store different panels.
In one panel, I need to draw the grid using imported data. I notice that if the grid is under a frame, it automatically supports scrolling, while if the parent is a normal panel, it can not be scrolled.
Then I found the wxScrolledWindow which satisfies my needs, but it seems the mousewheel event does not propagate to its parent, so I tried the Bind function to propagate event like this:
Code: Select all
wxGrid* grid = new wxGrid(scrolledWindow, wxID_ANY);
grid->CreateGrid(100, 100);
grid->Bind(wxEVT_MOUSEWHEEL, [&](wxMouseEvent& ev)
{
wxScrolledWindow* scrolledWindow = dynamic_cast<wxScrolledWindow*>(GetParent());
if (scrolledWindow)
{
// Pass the event to the parent wxScrolledWindow
scrolledWindow->GetEventHandler()->ProcessEvent(ev);
}
});
Code: Select all
class MyScrolledGrid : public wxGrid
{
public:
MyScrolledGrid(wxWindow* parent, wxWindowID id)
: wxGrid(parent, id)
{
}
void OnMouseWheel(wxMouseEvent& event)
{
wxScrolledWindow* scrolledWindow = dynamic_cast<wxScrolledWindow*>(GetParent());
if (scrolledWindow)
{
// Pass the event to the parent wxScrolledWindow
scrolledWindow->GetEventHandler()->ProcessEvent(event);
}
}
wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(MyScrolledGrid, wxGrid)
EVT_MOUSEWHEEL(MyScrolledGrid::OnMouseWheel)
wxEND_EVENT_TABLE()
Due to my limited knowledge, I cannot find the difference between these two implementation, could you please help me understand it?
Plus, ChatGPT also advices me to add a
Code: Select all
scrolledWindow->SetFocus();