Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#17681 closed defect (invalid)

wxButton::SetDefault doesn't work in some dialogs under wxGTK

Reported by: obfuscated Owned by:
Priority: normal Milestone:
Component: wxGTK Version: 3.0.2
Keywords: Cc:
Blocked By: Blocking:
Patch: no


Sometimes it works, but sometimes it doesn't. It seems this happens when there is a box sizer full of buttons in the dialog.

To reproduce:

  1. apply the attached patch
  2. build the minimal sample
  3. place the resources.xrc file next to the executable
  4. run minimal
  5. file -> dialog

Expected - the ok button to be default, but instead the add button is default.

It seems that both wx3.0.2 and master are affected by this. The same code seems to work fine with wx2.8.12.

Attachments (2)

0001-patch-to-reproduce-SetDefault-problem.patch download (33.6 KB) - added by obfuscated 5 years ago.
patch for minimal
resource.xrc download (2.0 KB) - added by obfuscated 5 years ago.

Download all attachments as: .zip

Change History (9)

Changed 5 years ago by obfuscated

patch for minimal

Changed 5 years ago by obfuscated

comment:1 Changed 5 years ago by obfuscated

gtk+ installed is 2.24.31. Running on Gentoo Linux amd64

comment:2 Changed 4 years ago by pcor

  • Status changed from new to confirmed

It looks to me like SetDefault() doesn't work at all with wxGTK. Whenever it appears to work, that's probably just where the focus was going to be anyway.

diff --git a/samples/dialogs/dialogs.cpp b/samples/dialogs/dialogs.cpp
index 7ddc670..e458a04 100644
--- a/samples/dialogs/dialogs.cpp
+++ b/samples/dialogs/dialogs.cpp
@@ -3117,8 +3117,7 @@ MyModalDialog::MyModalDialog(wxWindow *parent)
-    m_btnModal->SetFocus();
-    m_btnModal->SetDefault();
+    m_btnDelete->SetDefault();

It's unclear to me what the point of SetDefault() is. It seems like whichever control has the focus will get the return key press. Why not just use SetFocus()?

comment:3 Changed 4 years ago by vadz

In MSW, the default button is the one that is activated by "Enter" key when a non-button control has focus. The last clause is important because, indeed, if the focus is on a button, pressing "Enter" would just activate this button anyhow.

I'm not sure what's the convention in GTK, but for both MSW and Mac it's important to set the default button correctly because it has a different appearance to the other ones.

comment:4 Changed 4 years ago by obfuscated

  • Cc fuscated@… added

GTK3 has the notion of default buttons -

This is the UI definition for the gtk widget factory and the snippet bellow seems to be for the act dialog. It is very similar to the act dialog in the HIG page.

      <action-widget response="cancel">cancel_action_dialog</action-widget>
      <action-widget response="ok" default="true">act_action_dialog</action-widget>

I'm not sure there is similar notion/property in gtk2.

comment:5 Changed 4 years ago by pcor

  • Resolution set to invalid
  • Status changed from confirmed to closed

SetDefault() seems to work just fine. If I click on the listbox in your example, then press "Enter", the "OK" button is activated. SetDefault() and SetFocus() are not the same thing.

comment:6 Changed 4 years ago by obfuscated

  • Cc fuscated@… removed

I'm not sure I remember what the original problem was. So I don't have a concrete example, but I think it is related to the usability problem we have in Code::Blocks that when a dialog is opened by some menu item pressing escape sometimes closes it sometimes doesn't. It seems if depends on the focused item.

Are you saying that we should always call setfocus to something in the dialog to make sure this problem never happens?

comment:7 Changed 4 years ago by pcor

The documentation for SetDefault() only mentions the return key. I think escape key handling is a different thing, and your example code doesn't seem to demonstrate a problem with it. No matter where I set the focus, escape closes the dialog.

Note: See TracTickets for help on using tickets.