Package libxyz :: Package ui :: Module xyzlistbox

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.ui.xyzlistbox

      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  from libxyz.ui import Border
     20  from libxyz.ui import Keys
     21
     22 -class XYZListBox(lowui.WidgetWrap):
     23      """
     24      Simple list box
     25      """
     26
     27      # Skin rulesets resolution order
     28      resolution = (u"list_box", u"box", u"widget")
     29
     30 -    def __init__(self, xyz, body, walker, title, dim=None):
     31          """
     32          @param xyz: XYZ data
     33          @param body: Top-level widget
     34          @param walker: SimpleWalker or any walker-like instance
     35          @param title: ListBox title
     36
     37          Required resources: title, border, box, selected
     38          """
     39
     40          self.xyz = xyz
     41
     42          if dim is None:
     43              _dim = self._get_dim()
     44          else:
     45              _dim = dim
     46
     47          self._walker = walker
     48          self._keys = Keys()
     49
     50          self.listbox = lowui.ListBox(walker)
     51          self.title = title
     52          self._listbox = lowui.AttrWrap(self.listbox, self._attr(u"box"))
     53
     54          self._box = Border(self._listbox, title, self._attr(u"title"),
     55                             self._attr(u"border"))
     56
     57          _box = lowui.Overlay(self._box, body, align.CENTER, _dim[0],
     58                               align.MIDDLE, _dim[1])
     59
     60          super(XYZListBox, self).__init__(_box)
     61
     62      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     63
     64 -    def show(self, dim=None, exit_keys=None):
     65          """
     66          Show list
     67          """
     68
     69          exit_keys = exit_keys or []
     70
     71          if dim is None:
     72              dim = self.xyz.screen.get_cols_rows()
     73
     74          while True:
     75              self.xyz.screen.draw_screen(dim, self.render(dim, True))
     76
     77              _i = self.xyz.input.get()
     78
     79              if self.xyz.input.WIN_RESIZE in _i:
     80                  dim = self.xyz.screen.get_cols_rows()
     81                  continue
     82
     83              if _i:
     84                  for _k in _i:
     85                      if _k == self._keys.ESC:
     86                          return
     87                      elif _k == "j":
     88                          _k = self._keys.DOWN
     89                      elif _k == "k":
     90                          _k = self._keys.UP
     91
     92                      self._listbox.keypress(dim, _k)
     93
     94                      # Also quit on specified keys if any
     95                      if _k in exit_keys:
     96                          return
     97
     98      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     99
    100 -    def set_title(self, title):
    101          """
    102          Change title
    103          """
    104
    105          self.title = title
    106          self._box.set_title(title)
    107
    108      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    109
    110 -    def _attr(self, name):
    111          """
    112          Find palette
    113          """
    114
    115          return self.xyz.skin.attr(self.resolution, name)
    116
    117      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    118
    119 -    def _get_dim(self):
    120          _dim = self.xyz.screen.get_cols_rows()
    121
    122          return (_dim[0] - 4, _dim[1] - 4)
    123