From d2bbd784f21286d1fb5e242a61c6266e2ffdf034 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Wed, 1 Mar 2017 14:42:03 +0000 Subject: [PATCH] TextEditDialog has tab-navigation, and enter is equal to clicking Ok. --- fsl/utils/dialog.py | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/fsl/utils/dialog.py b/fsl/utils/dialog.py index ae57a61ba..fd1886daa 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. """ -- GitLab