+
+class TextBoxWidget(TextWidget):
+ def __init__(self, *args, **kwargs):
+ self.padding = kwargs.pop('padding', 5)
+ self.border = kwargs.pop('border', 2)
+ self.bg_colour = convert_colour(kwargs.pop('bg_colour',
+ (255, 255, 255, 192)))
+ self.border_colour = convert_colour(kwargs.pop('border_colour',
+ (0, 0, 0)))
+ self.box_width = kwargs.pop('box_width', 0)
+
+ super(TextBoxWidget, self).__init__(*args, **kwargs)
+
+ def lines(self):
+ if self.box_width != 0:
+ return self._wrapped_lines()
+ else:
+ return self.text.splitlines()
+
+ def _wrapped_lines(self):
+ def words_fit(words):
+ words_line = ' '.join(words)
+ width = self.font.size(words_line)[0]
+ if width < self.box_width:
+ return True
+ elif len(words) == 1:
+ Exception("Word %r too long for box." % (words[0],))
+ return False
+
+ for line in self.text.splitlines():
+ current_words = []
+ for word in line.split():
+ current_words.append(word)
+ if words_fit(current_words):
+ continue
+ else:
+ yield ' '.join(current_words[:-1])
+ current_words = current_words[-1:]
+ if current_words and words_fit(current_words):
+ yield ' '.join(current_words)
+
+ def prepare(self):