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.

91 lines
2.7 KiB

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