diff --git a/fsl/utils/dialog.py b/fsl/utils/dialog.py index ae57a61ba7a52cebfe49baea10959872759a9b8f..fd1886daaeef719737b3f54775732bd95282f928 100644 --- a/fsl/utils/dialog.py +++ b/fsl/utils/dialog.py @@ -450,10 +450,11 @@ class TextEditDialog(wx.Dialog): # ctrl so it can fit a few lines self.__textEdit.SetMinSize((-1, 120)) - self.__ok = (-1, -1) - self.__copy = (-1, -1) - self.__cancel = (-1, -1) - self.__icon = (-1, -1) + self.__ok = (-1, -1) + self.__copy = (-1, -1) + self.__cancel = (-1, -1) + self.__icon = (-1, -1) + self.__buttons = [] if icon is not None: @@ -471,14 +472,19 @@ class TextEditDialog(wx.Dialog): if style & TED_OK: self.__ok = wx.Button(self, id=wx.ID_OK) self.__ok.Bind(wx.EVT_BUTTON, self.__onOk) + self.__buttons.append(self.__ok) if style & TED_CANCEL: self.__cancel = wx.Button(self, id=wx.ID_CANCEL) self.__cancel.Bind(wx.EVT_BUTTON, self.__onCancel) + self.__buttons.append(self.__cancel) if style & TED_COPY: self.__copy = wx.Button(self, label='Copy to clipboard') - self.__copy.Bind(wx.EVT_BUTTON, self.__onCopy) + self.__copy.Bind(wx.EVT_BUTTON, self.__onCopy) + self.__buttons.append(self.__copy) + + self.__textEdit.Bind(wx.EVT_CHAR_HOOK, self.__onCharHook) textSizer = wx.BoxSizer(wx.VERTICAL) iconSizer = wx.BoxSizer(wx.HORIZONTAL) @@ -514,6 +520,28 @@ class TextEditDialog(wx.Dialog): self.SetSizer(mainSizer) self.Fit() + + def __onCharHook(self, ev): + """Called on ``EVT_CHAR_HOOK`` events generated by the ``TextCtrl``. + Implements tab-navigation, and makes the enter key behave as if + the user had clicked the OK button. + """ + + key = ev.GetKeyCode() + + if key not in (wx.WXK_TAB, wx.WXK_RETURN): + ev.Skip() + return + + # Dodgy, but I've had loads of trouble + # under OSX - Navigate/HandleAsNavigationKey + # do not work. + if key == wx.WXK_TAB and len(self.__buttons) > 0: + self.__buttons[0].SetFocus() + + elif key == wx.WXK_RETURN: + self.__onOk(None) + def __onOk(self, ev): """Called when the *Ok* button is pressed. Ends the dialog. """