Reputation: 2734
I've got a label with a mnemonic that precedes a custom widget - just a compound widget that derives from wxPanel, nothing too fancy. See below picture.
The problem is, I can't seem to get the mnemonic to work with that.
What I naively expect is for the custom widget to gain focus, but it doesn't seem to be the case.
What is the mechanism behind the mnemonics, then? How should a custom widget be made to work with them?
EDIT: adding a minimal sample code to illustrate the issue.
It's a diff against the minimal.cpp sample program, found in the v3.0.5 branch of the repository.
Tested on Linux, with GTK3. Configure options: --enable-shared --disable-static --enable-unicode --enable-printfposparam --enable-debug --with-gtk=3
Diff against minimal.cpp:
diff --git a/samples/minimal/minimal.cpp b/samples/minimal/minimal.cpp
index 135595c9fb..b656000879 100644
--- a/samples/minimal/minimal.cpp
+++ b/samples/minimal/minimal.cpp
@@ -172,6 +172,26 @@ MyFrame::MyFrame(const wxString& title)
CreateStatusBar(2);
SetStatusText("Welcome to wxWidgets!");
#endif // wxUSE_STATUSBAR
+
+ wxPanel *root = new wxPanel(this); {
+ wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
+
+ wxStaticText *label = new wxStaticText(root, wxID_ANY, wxS("T&est"));
+ wxPanel *panel = new wxPanel(root); {
+ wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
+ wxTextCtrl *text = new wxTextCtrl(panel, wxID_ANY);
+ sizer->Add(text, wxSizerFlags(0).Expand());
+ panel->SetSizerAndFit(sizer);
+ }
+
+ wxTextCtrl *text2 = new wxTextCtrl(root, wxID_ANY);
+
+ sizer->Add(label, wxSizerFlags(0).Expand());
+ sizer->Add(panel, wxSizerFlags(0).Expand());
+ sizer->Add(text2, wxSizerFlags(0).Expand());
+
+ root->SetSizerAndFit(sizer);
+ }
}
What I expect is to be able to press ALT+e and have the first wxTextCtrl focused, but instead the second one gets the focus. If I remove the second one from the code, I get the following line on the console:
(minimal:2409662): Gtk-WARNING **: 01:58:45.676: Couldn't find a target for a mnemonic activation.
EDIT2: the previous code I posted wouldn't work at all on Windows, displaying other kind of problems. Turned out that I needed to use a root wxPanel to embed the widgets, rather than putting them in the wxFrame directly. Having done that modification, the code above now works as expected with the Windows target. So the problem seems to be with GTK (3?).
Upvotes: 1
Views: 188
Reputation: 22753
Unfortunately there is indeed a problem with using mnemonic in a wxStaticText
before, rather than inside, a wxPanel
in wxGTK. 3.1 gives a more useful warning message:
widget 'wxPizza' isn't suitable for mnemonic activation
and looking at GTK source code of gtk_widget_real_mnemonic_activate()
this is due to gtk_widget_get_can_focus()
returning false for wxPanel
itself, which seems to make sense because the panel is not supposed to take focus itself, only its children are, but clearly goes against GTK expectations.
For now I'm afraid I can only recommend putting the label inside the panel, because even though I've created a ticket for this problem and there might be a way to fix it, it's not trivial and so risks not happening in the near future.
Upvotes: 1