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.

103 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
  1. import webbrowser
  2. import wx
  3. from gooey.gui.lang.i18n import _
  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=_("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. # The rich console provides the embedded URL via GetString()
  34. # but the basic console does not
  35. webbrowser.open(
  36. event.GetString() or
  37. self.textbox.GetRange(event.URLStart,event.URLEnd))
  38. event.Skip()
  39. def getFontStyle(self):
  40. """
  41. Force wx.Modern style to support legacy
  42. monospace_display param when present
  43. """
  44. return (wx.MODERN
  45. if self.buildSpec['monospace_display']
  46. else wx.DEFAULT)
  47. def getFontFace(self):
  48. """Choose the best font face available given the user options"""
  49. userFace = self.buildSpec['terminal_font_family'] or self.defaultFont.GetFaceName()
  50. return (''
  51. if self.buildSpec['monospace_display']
  52. else userFace)
  53. def logOutput(self, *args, **kwargs):
  54. """Event Handler for console updates coming from the client's program"""
  55. self.appendText(kwargs.get('msg'))
  56. def appendText(self, txt):
  57. """
  58. Append the text to the main TextCtrl.
  59. Note! Must be called from a Wx specific thread handler to avoid
  60. multi-threaded explosions (e.g. wx.CallAfter)
  61. """
  62. self.textbox.AppendText(txt)
  63. def clear(self):
  64. """
  65. Clear the the main TextCtrl.
  66. """
  67. self.textbox.Clear()
  68. def getText(self):
  69. return self.textbox.GetValue()
  70. def layoutComponent(self):
  71. self.SetBackgroundColour(self.buildSpec.get('terminal_bg_color', '#F0F0F0'))
  72. sizer = wx.BoxSizer(wx.VERTICAL)
  73. sizer.AddSpacer(10)
  74. sizer.Add(self.text, 0, wx.LEFT, 20)
  75. sizer.AddSpacer(10)
  76. sizer.Add(self.textbox, 1, wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, 20)
  77. sizer.AddSpacer(20)
  78. self.SetSizer(sizer)