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.

99 lines
3.0 KiB

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