# HG changeset patch # User Neil Muller # Date 1378508635 -7200 # Node ID 450081926426746a48adf9399561dca482675429 # Parent 76f053cf2322aa3dfc8ec03d1ceda8378928a50e Better MultiLineWidget diff -r 76f053cf2322 -r 450081926426 nagslang/widgets/text.py --- a/nagslang/widgets/text.py Sat Sep 07 01:10:53 2013 +0200 +++ b/nagslang/widgets/text.py Sat Sep 07 01:03:55 2013 +0200 @@ -31,26 +31,6 @@ surface.blit(self.surface, self.rect) -class MultiLineWidget(TextWidget): - - def prepare(self): - self.font = resources.get_font(self.fontname, self.fontsize) - surfaces = [] - height = 0 - width = 0 - for line in self.text.split('\n'): - surface = self.font.render(line, True, self.colour) - width = max(width, surface.get_rect().width) - height += surface.get_rect().height - surfaces.append(surface) - self.surface = pygame.surface.Surface((width, height)) - self.surface.fill(pygame.Color('white')) - y = 0 - for surface in surfaces: - self.surface.blit(surface, (0, y)) - y += surface.get_rect().height - - class LabelWidget(TextWidget): def __init__(self, *args, **kwargs): self.padding = kwargs.pop('padding', 5) @@ -74,3 +54,27 @@ pygame.draw.rect(surface, self.border_colour, surface.get_rect(), self.border) self.surface = surface + + +class MultiLineWidget(LabelWidget): + + def prepare(self): + self.font = resources.get_font(self.fontname, self.fontsize) + surfaces = [] + height = 0 + width = 0 + for line in self.text.split('\n'): + surface = self.font.render(line, True, self.colour) + width = max(width, surface.get_rect().width) + height += surface.get_rect().height + surfaces.append(surface) + self.surface = pygame.surface.Surface((width, height), + pygame.locals.SRCALPHA) + self.surface.fill(self.bg_colour) + y = 0 + for surface in surfaces: + self.surface.blit(surface, (0, y)) + y += surface.get_rect().height + self.text_rect = self.surface.get_rect() + if not self.size: + self.rect.size = self.text_rect.size