Package libxyz :: Package ui :: Module box

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.ui.box

      1  #-*- coding: utf8 -*
      2  #
      3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008
      4  #
      5  # This file is part of XYZCommander.
      6  # XYZCommander is free software: you can redistribute it and/or modify
      7  # it under the terms of the GNU Lesser Public License as published by
      8  # the Free Software Foundation, either version 3 of the License, or
      9  # (at your option) any later version.
     10  # XYZCommander is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  # GNU Lesser Public License for more details.
     14  # You should have received a copy of the GNU Lesser Public License
     15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
     16
     17  from libxyz.ui import lowui
     18  from libxyz.ui import align
     19
     20 -class Box(lowui.WidgetWrap):
     21      """
     22      Base box
     23      """
     24
     25      # Skin rulesets resolution order
     26      resolution = (u"box", u"widget")
     27
     28 -    def __init__(self, xyz, body, message, title="", width=70):
     29          """
     30          @param xyz: XYZ data
     31          @param body: Top-level widget
     32          @param message: Message to display
     33          @param title: Box title
     34          @param width: Box width
     35
     36          Required resources: title, box, mount
     37          """
     38
     39          self.xyz = xyz
     40          self.screen = xyz.screen
     41          self.skin = xyz.skin
     42          self.message = message
     43          self.full_width = width
     44          self._enc = xyzenc
     45
     46          self.mount_span = {u"vertical": 2, u"horizontal": 2}
     47
     48          self._attr = lambda name: self.skin.attr(self.resolution, name)
     49
     50      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     51
     52 -    def calc_size(self, rowspan):
     53          """
     54          Calculate size
     55          """
     56
     57          self.rowspan = rowspan
     58          self.box_width = self.full_width - self.mount_span[u"horizontal"]
     59          self.box_height = self._rows(self.message)
     60          self.full_height = self.box_height + self.mount_span[u"vertical"]
     61
     62      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     63
     64 -    def parent_init(self, box):
     65          """
     66          Init parent class
     67          """
     68
     69          super(Box, self).__init__(box)
     70
     71      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     72
     73 -    def show(self, dim=None, wait=True):
     74          """
     75          Show box
     76          @param dim: Dimension
     77          @param wait: If True wait for key pressed
     78          """
     79
     80          if dim is None:
     81              dim = self.screen.get_cols_rows()
     82
     83          self.screen.draw_screen(dim, self.render(dim, True))
     84
     85          _input = None
     86
     87          if wait:
     88              while True:
     89                  _input = self.xyz.input.get()
     90
     91                  if _input:
     92                      break
     93
     94          return _input
     95
     96      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     97
     98 -    def _rows(self, msg):
     99          """
    100          Calculate required rows
    101          """
    102
    103          # 2 for two rows: on top and bottom
    104          _maxrows = self.screen.get_cols_rows()[1] - \
    105                     2 - self.mount_span[u"vertical"]
    106          _lines = msg.count("\n")
    107
    108          if _lines + self.rowspan > _maxrows:
    109              _rows = _maxrows
    110          else:
    111              _rows = _lines + self.rowspan
    112
    113          return _rows
    114
    115      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    116
    117 -    def _strip_title(self, title):
    118          """
    119          Strip title if needed
    120          """
    121
    122          _maxlen = self.box_width - 6
    123          _len = len(title)
    124
    125          _stripped = title
    126
    127          if _len >= _maxlen:
    128              _stripped = u"%s..." % title[:_maxlen - 3]
    129
    130          return _stripped
    131