Hey all-

Just wrote this and thought it might be useful to the soya.pudding users out there.  It's a Label that you can scroll on using up/down pageup/pagedown and the mouse wheel.  Requires wrap = True

Comments, corrections, and additions are welcome. 


class ScrollableLabel(pudding.control.Label):
    def __init__(self, parent=None, label='', **kwargs):
        self.start_line = 0
        self.visible_lines = 0
        self.max_height = 0
        pudding.control.Label.__init__(self, parent=parent, label=label, **kwargs)
   
    def update(self):
        """ refresh settings based on clip and autoresize etc """

        if len(self._text) > pudding.control.SimpleLabel.MAXLEN:
            self._text = self._text[-pudding.control.SimpleLabel.MAXLEN:]
       
        self.needs_update = True
       
        self.set_display_text( self._text )

        if self._wrap:
            max_width, height, self._display_text = \
                self._font.wordwrap(self._display_text, self._width)

            if self._autosize:
                self.height = height
   
            self.text_width = max_width
            self.text_height = min(self.height, height)
            self.max_height = max(self.height, height)
       
            if height > self.height:
                lines_to_many = (height - self._height) / self._font.height
                self.visible_lines = (self._height) / self._font.height
                last_line = self.start_line + self.visible_lines
                self._display_text = "\n".join(
                            self._display_text.split('\n')[self.start_line:last_line]
                            )

        elif self._autosize:
            self._width, self._height = \
                self._font.get_print_size(self._display_text)
   
            self.text_width, self.text_height = self._width, self._height
    
        elif self._clip != CLIP_NONE:
            width, height = self._font.get_print_size(self._display_text)
    
            while width > self.width:
                if self._clip == CLIP_LEFT:
                    self._display_text = self._display_text[1:]
                else:
                    self._display_text = self._display_text[:-1]
    
                width, height = self._font.get_print_size(self._display_text)
    
            self.text_width, self.text_height = width, height
   
    def process_event(self, event):
        pudding.control.Label.process_event(self,event)
        return True
           
    def on_key_down(self, key, mods):
        if key == soya.sdlconst.K_DOWN:
            self.start_line += 1
            if self.start_line > self.max_height/self._font.height - self.visible_lines:
                self.start_line = int(self.max_height/self._font.height - self.visible_lines)
            self.update()
        elif key == soya.sdlconst.K_UP and self.start_line > 0:
            self.start_line -= 1
            self.update()
        elif key == soya.sdlconst.K_PAGEDOWN:
            self.start_line += self.visible_lines
            if self.start_line > self.max_height/self._font.height - self.visible_lines:
                self.start_line = int(self.max_height/self._font.height - self.visible_lines)
            self.update()
        elif key == soya.sdlconst.K_PAGEUP:
            self.start_line -= self.visible_lines
            if self.start_line < 0:
                self.start_line = 0
            self.update()

    def on_mouse_down(self, x, y, button):
        if button == soya.sdlconst.BUTTON_WHEELDOWN:
            self.start_line += 1
            if self.start_line > self.max_height/self._font.height - self.visible_lines:
                self.start_line = int(self.max_height/self._font.height - self.visible_lines)
            self.update()
        elif button == soya.sdlconst.BUTTON_WHEELUP and self.start_line > 0:
            self.start_line -= 1
            self.update()

Reply via email to