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.

82 lines
2.5 KiB

  1. import wx
  2. from gooey.gui.lang import i18n
  3. class Console(wx.Panel):
  4. '''
  5. Textbox console/terminal displayed during the client program's execution.
  6. '''
  7. def __init__(self, parent, buildSpec, **kwargs):
  8. wx.Panel.__init__(self, parent, **kwargs)
  9. self.buildSpec = buildSpec
  10. self.text = wx.StaticText(self, label=i18n._("status"))
  11. self.textbox = wx.TextCtrl(
  12. self, -1, "", style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_RICH
  13. )
  14. self.defaultFont = self.textbox.GetFont()
  15. self.textbox.SetFont(wx.Font(
  16. self.buildSpec['terminal_font_size'] or self.defaultFont.GetPointSize(),
  17. self.getFontStyle(),
  18. wx.NORMAL,
  19. self.buildSpec['terminal_font_weight'] or wx.NORMAL,
  20. False,
  21. self.getFontFace(),
  22. ))
  23. self.textbox.SetForegroundColour(self.buildSpec['terminal_font_color'])
  24. self.layoutComponent()
  25. self.Layout()
  26. def getFontStyle(self):
  27. """
  28. Force wx.Modern style to support legacy
  29. monospace_display param when present
  30. """
  31. return (wx.MODERN
  32. if self.buildSpec['monospace_display']
  33. else wx.DEFAULT)
  34. def getFontFace(self):
  35. """Choose the best font face available given the user options"""
  36. userFace = self.buildSpec['terminal_font_family'] or self.defaultFont.GetFaceName()
  37. return (''
  38. if self.buildSpec['monospace_display']
  39. else userFace)
  40. def logOutput(self, *args, **kwargs):
  41. """Event Handler for console updates coming from the client's program"""
  42. self.appendText(kwargs.get('msg'))
  43. def appendText(self, txt):
  44. """
  45. Append the text to the main TextCtrl.
  46. Note! Must be called from a Wx specific thread handler to avoid
  47. multi-threaded explosions (e.g. wx.CallAfter)
  48. """
  49. self.textbox.AppendText(txt)
  50. def getText(self):
  51. return self.textbox.GetValue()
  52. def layoutComponent(self):
  53. self.SetBackgroundColour(self.buildSpec.get('terminal_bg_color', '#F0F0F0'))
  54. sizer = wx.BoxSizer(wx.VERTICAL)
  55. sizer.AddSpacer(10)
  56. sizer.Add(self.text, 0, wx.LEFT, 20)
  57. sizer.AddSpacer(10)
  58. sizer.Add(self.textbox, 1, wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, 20)
  59. sizer.AddSpacer(20)
  60. self.SetSizer(sizer)