You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

2 years ago
2 years ago
2 years ago
  1. import os
  2. import wx # type: ignore
  3. from functools import reduce
  4. from gooey.gui.components.widgets.core.text_input import MultilineTextInput
  5. from gooey.gui.components.widgets.textfield import TextField
  6. from gooey.gui.components.widgets.bases import TextContainer
  7. from gooey.gui import formatters
  8. from gooey.python_bindings import types as t
  9. from gooey.python_bindings.types import FormField
  10. class Textarea(TextContainer):
  11. def getWidget(self, parent, *args, **options):
  12. widgetHeight = self._options.get('height', -1)
  13. return wx.TextCtrl(
  14. parent=parent,
  15. size=(-1, widgetHeight),
  16. style=self.getModifiers()
  17. )
  18. def getModifiers(self):
  19. readonly = (wx.TE_READONLY
  20. if self._options.get('readonly', False)
  21. # using TE_MUTLI as a safe OR-able no-op value
  22. else wx.TE_MULTILINE)
  23. return reduce(lambda acc, val: acc | val, [wx.TE_MULTILINE, readonly])
  24. def getWidgetValue(self):
  25. return self.widget.GetValue()
  26. def setValue(self, value):
  27. self.widget.Clear()
  28. self.widget.AppendText(str(value))
  29. self.widget.SetInsertionPoint(0)
  30. def formatOutput(self, metatdata, value: str):
  31. return formatters.general(metatdata, value.replace('\n', os.linesep))
  32. def syncUiState(self, state: FormField):
  33. self.setValue(state['value']) # type: ignore
  34. self.error.SetLabel(state['error'] or '')
  35. self.error.Show(state['error'] is not None and state['error'] is not '')
  36. def getUiState(self) -> t.FormField:
  37. return t.TextField(
  38. id=self._id,
  39. type=self.widgetInfo['type'],
  40. value=self.getWidgetValue(),
  41. placeholder=self.widget.GetHint(),
  42. error=self.error.GetLabel().replace('\n', ' '),
  43. enabled=self.IsEnabled(),
  44. visible=self.IsShown()
  45. )