diff --git a/source/gui/guiHelper.py b/source/gui/guiHelper.py index e0ccd3bcbc9..b33edad2875 100644 --- a/source/gui/guiHelper.py +++ b/source/gui/guiHelper.py @@ -45,7 +45,7 @@ def __init__(self,parent): from contextlib import contextmanager import wx -from wx.lib import scrolledpanel +from wx.lib import scrolledpanel, newevent from abc import ABCMeta #: border space to be used around all controls in dialogs @@ -159,17 +159,49 @@ class LabeledControlHelper(object): the two controls together. Relies on guiHelper.associateElements(), any limitations in guiHelper.associateElements() also apply here. """ - def __init__(self, parent, labelText, wxCtrlClass, **kwargs): + + # When the control is enabled / disabled this event is raised. + # A handler is automatically added to the control to ensure the label is also enabled/disabled. + EnableChanged, EVT_ENABLE_CHANGED = newevent.NewEvent() + + def __init__(self, parent: wx.Window, labelText: str, wxCtrlClass: wx.Control, **kwargs): """ @param parent: An instance of the parent wx window. EG wx.Dialog @param labelText: The text to associate with a wx control. - @type labelText: string @param wxCtrlClass: The class to associate with the label, eg: wx.TextCtrl - @type wxCtrlClass: class @param kwargs: The keyword arguments used to instantiate the wxCtrlClass """ object.__init__(self) - self._label = wx.StaticText(parent, label=labelText) - self._ctrl = wxCtrlClass(parent, **kwargs) + + class LabelEnableChangedListener(wx.StaticText): + isDestroyed = False + isListening = False + + def _onDestroy(self, evt: wx.WindowDestroyEvent): + self.isDestroyed = True + + def listenForEnableChanged(self, _ctrl: wx.Window): + self.Bind(wx.EVT_WINDOW_DESTROY, self._onDestroy) + _ctrl.Bind(LabeledControlHelper.EVT_ENABLE_CHANGED, self._onEnableChanged) + self.isListening = True + + def _onEnableChanged(self, evt: wx.Event): + if self.isListening and not self.isDestroyed: + self.Enable(evt.isEnabled) + + class WxCtrlWithEnableEvnt(wxCtrlClass): + def Enable(self, enable=True): + evt = LabeledControlHelper.EnableChanged(isEnabled=enable) + wx.PostEvent(self, evt) + super().Enable(enable) + + def Disable(self): + evt = LabeledControlHelper.EnableChanged(isEnabled=False) + wx.PostEvent(self, evt) + super().Disable() + + self._label = LabelEnableChangedListener(parent, label=labelText) + self._ctrl = WxCtrlWithEnableEvnt(parent, **kwargs) + self._label.listenForEnableChanged(self._ctrl) self._sizer = associateElements(self._label, self._ctrl) @property diff --git a/user_docs/en/changes.t2t b/user_docs/en/changes.t2t index e5ba895cf3c..22f845c98a0 100644 --- a/user_docs/en/changes.t2t +++ b/user_docs/en/changes.t2t @@ -27,6 +27,7 @@ What's New in NVDA - In Mozilla Firefox and Chromium based browsers, updates to ARIA live regions are now suppressed when reporting of dynamic content changes is disabled. (#9077) - NVDA will now report "Copied to clipboard" before the copied text. (#6757) - Presentation of graphical view table in disk management has been improved. (#10048) +- Labels for controls are now disabled (greyed out) when the control is disabled. (#11809) == Bug Fixes ==