Changeset 148

Show
Ignore:
Timestamp:
24.03.2008 20:29:26 (10 months ago)
Author:
elghinn
Message:

* added support for long menu

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/pycawm/menu.py

    r147 r148  
    7979        self.font = self.display.open_font('fixed') 
    8080        self.heights = list() 
     81        self.height = 0 
    8182        self.width = 0 
     83        plus_extents = self.font.query_text_extents('Plus... >') 
     84        plus_height = (plus_extents.overall_ascent + 
     85                       plus_extents.overall_descent + 2) 
     86        plus_index = -1 
     87        screen_height = self.wm.screen.height_in_pixels - 1 
    8288        for i, element in enumerate(self.elements): 
    8389            if element[1] == self.separator and not element[0]: 
    8490                self.heights.append(4) 
     91                self.height += 4 
    8592            else: 
    8693                element = list(element) 
     
    9097                self.elements[i] = element 
    9198                extents = self.font.query_text_extents(element[0]) 
    92                 self.heights.append( 
    93                     extents.overall_ascent + extents.overall_descent + 2) 
     99                height = extents.overall_ascent + extents.overall_descent + 2 
     100                # if we don't have enough space for the current item, we cut 
     101                # the menu, and append a "Plus..." submenu at the first part 
     102                # with the rest of the menu 
     103                if not ((i + 1 == len(self.elements) and 
     104                         height + self.height < screen_height) or 
     105                        height + self.height + plus_height < screen_height): 
     106                    plus_index = i 
     107                    break 
     108                self.heights.append(height) 
     109                self.height += height 
    94110                self.width = max(self.width, extents.overall_width) 
    95         self.height = sum(self.heights) 
     111        # if we have to cut the menu, append the "Plus..." submenu 
     112        if plus_index > -1: 
     113            sub_elements = self.elements[plus_index:] 
     114            self.elements = self.elements[:plus_index] 
     115            self.elements.append( 
     116                ('Plus... >', Menu.sub_menu, 
     117                 lambda menu, x, y: Menu(self.wm, parent=self, 
     118                                         x=x, y=y, 
     119                                         elements=sub_elements))) 
     120            self.heights.append(plus_height) 
     121            self.height += plus_height 
     122            self.width = max(self.width, plus_extents.overall_width) 
     123        # if menu going out of the bottom of screen, enhance it 
     124        if self.height + self.y >= screen_height: 
     125            self.y = screen_height - self.height - 1 
    96126        self.width += 1 
    97127