Discussion:
Forward events to other widgets
Jan Engelhardt
2014-04-03 14:10:05 UTC
Permalink
I am looking for a way to navigate in a ListCtrl while another widget
has focus. A TextCtrl gets CHAR events as usual. There is just a
single line in this TextCtrl, so WXK_UP/WXK_DOWN does not have much
of a meaning. In fact, GTK2 "abuses" it to jump to the next widget
like TAB normally would.

Anyhow, I would like to use WXK_UP/DOWN for moving around the
ListCtrl. It would seem that wxPostEvent and ProcessEvent are both
ignored. The code below is an example of that.

This may have to do with the fact that wxListMainWindow is separate
from wxListCtrl itself. If possible, I would rather avoid having to
copy what's in wxListMainWindow::OnChar.

#include <wx/wx.h>
#include <wx/listctrl.h>
#include <cstdio>
class M : public wxFrame {
public:
wxTextCtrl *text;
wxListCtrl *list;

public: M(void) : wxFrame(NULL, wxID_ANY, "") {
text = new wxTextCtrl(this, wxID_ANY);
text->Bind(wxEVT_CHAR, [this](wxKeyEvent &event) {
auto k = event.GetKeyCode();
if (list != NULL && (k == WXK_UP || k == WXK_DOWN)) {
wxKeyEvent copy = event;
copy.SetId(list->GetId());
copy.SetEventObject(list);
wxPostEvent(list, copy);
//list->GetEventHandler()->ProcessEvent(copy);
return;
}
event.Skip();
});

list = new wxListCtrl(this, wxID_ANY);
list->AppendColumn("head");
list->InsertItem(0, "first");
list->InsertItem(1, "second");
list->SetInitialSize(wxSize(200, 200));

auto sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(text, 0, wxEXPAND);
sizer->Add(list, 1, wxEXPAND);
SetSizer(sizer);
sizer->SetSizeHints(this);
};
};
class A : public wxApp {
public: bool OnInit(void) {
(new M)->Show();
return true;
};
int FilterEvent(wxEvent &e) {
auto t = e.GetEventType();
if (t == wxEVT_CHAR) {
auto w = dynamic_cast<wxWindow *>(e.GetEventObject());
printf("CHAR for %d\n", w->GetId());
}
return Event_Skip;
};
};
IMPLEMENT_APP(A);
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-04-04 15:02:44 UTC
Permalink
On Thu, 3 Apr 2014 16:10:05 +0200 (CEST) Jan Engelhardt wrote:

JE> Anyhow, I would like to use WXK_UP/DOWN for moving around the
JE> ListCtrl. It would seem that wxPostEvent and ProcessEvent are both
JE> ignored.

In general this has no hope of working. Native controls don't know
anything about wx-level events, so posting them will never work. Of course,
wxListCtrl is generic in wxGTK currently, but this can (should...) change
in newer versions and is already not true under other platforms.

JE> This may have to do with the fact that wxListMainWindow is separate
JE> from wxListCtrl itself. If possible, I would rather avoid having to
JE> copy what's in wxListMainWindow::OnChar.

The way it's supposed to work is that you'd call Scroll{Lines,Pages}()
methods instead. But unfortunately they're not overridden/implemented by
wxGenericListCtrl currently. Any patches adding them would be welcome.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Loading...