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.

95 lines
3.7 KiB

  1. import wx
  2. from wx.lib.wordwrap import wordwrap
  3. class AutoWrappedStaticText(wx.StaticText):
  4. """
  5. Copy/pasta of wx.lib.agw.infobar.AutoWrapStaticText with 3 modifications:
  6. 1. Extends wx.StaticText rather than GenStaticText
  7. 2. Does not set the fore/background colors to sys defaults
  8. 3. takes an optional `target` parameter for sizing info
  9. The behavior of GenStaticText's background color is pretty buggy cross-
  10. platform. It doesn't reliably match its parent components background
  11. colors[0] (for instance when rendered inside of a Notebook) which leads to
  12. ugly 'boxing' around the text components.
  13. There is either a bug in WX, or or human error on my end, which causes
  14. EVT_SIZE events to continuously spawn from this (and AutoWrapStaticText) but
  15. with ever decreasing widths (in response to the SetLabel action in the
  16. wrap handler). The end result is a single skinny column of letters.
  17. The work around is to respond the EVT_SIZE event, but follow the size of the
  18. `target` component rather than relying on the size of the event.
  19. [0] more specifically, they'll match 1:1 on paper, but still ultimately
  20. render differently.
  21. """
  22. def __init__(self, parent, *args, **kwargs):
  23. self.target = kwargs.pop('target', None)
  24. super(AutoWrappedStaticText, self).__init__(parent, *args, **kwargs)
  25. self.label = kwargs.get('label')
  26. self.Bind(wx.EVT_SIZE, self.OnSize)
  27. def OnSize(self, event):
  28. """
  29. Handles the ``wx.EVT_SIZE`` event for :class:`AutoWrapStaticText`.
  30. :param `event`: a :class:`wx.SizeEvent` event to be processed.
  31. """
  32. event.Skip()
  33. if self.target:
  34. self.Wrap(self.target.GetSize().width)
  35. else:
  36. self.Wrap(event.GetSize().width)
  37. def Wrap(self, width):
  38. """
  39. This functions wraps the controls label so that each of its lines becomes at
  40. most `width` pixels wide if possible (the lines are broken at words boundaries
  41. so it might not be the case if words are too long).
  42. If `width` is negative, no wrapping is done.
  43. :param integer `width`: the maximum available width for the text, in pixels.
  44. :note: Note that this `width` is not necessarily the total width of the control,
  45. since a few pixels for the border (depending on the controls border style) may be added.
  46. """
  47. if width < 0:
  48. return
  49. self.Freeze()
  50. dc = wx.ClientDC(self)
  51. dc.SetFont(self.GetFont())
  52. text = wordwrap(self.label, width, dc)
  53. self.SetLabel(text, wrapped=True)
  54. self.Thaw()
  55. def SetLabel(self, label, wrapped=False):
  56. """
  57. Sets the :class:`AutoWrapStaticText` label.
  58. All "&" characters in the label are special and indicate that the following character is
  59. a mnemonic for this control and can be used to activate it from the keyboard (typically
  60. by using ``Alt`` key in combination with it). To insert a literal ampersand character, you
  61. need to double it, i.e. use "&&". If this behaviour is undesirable, use :meth:`~Control.SetLabelText` instead.
  62. :param string `label`: the new :class:`AutoWrapStaticText` text label;
  63. :param bool `wrapped`: ``True`` if this method was called by the developer using :meth:`~AutoWrapStaticText.SetLabel`,
  64. ``False`` if it comes from the :meth:`~AutoWrapStaticText.OnSize` event handler.
  65. :note: Reimplemented from :class:`wx.Control`.
  66. """
  67. if not wrapped:
  68. self.label = label
  69. wx.StaticText.SetLabel(self, label)