Package libxyz :: Package ui :: Module entry

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.ui.entry

      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.core.utils import ustring
     18  from libxyz.ui import lowui
     19  import libxyz.ui
     20
     21 -class ListEntry(lowui.FlowWidget):
     22      """
     23      List entry
     24      """
     25
     26 -    def __init__(self, msg, selected_attr, entry_attr=None):
     27          """
     28          @param msg: Message
     29          @param selected_attr: Atrribute of selected entry
     30          @param entry_attr: Entry text attribute
     31          """
     32
     33          super(ListEntry, self).__init__()
     34
     35          self._text = msg
     36          self._sel_attr = selected_attr
     37          self._entry_attr = entry_attr
     38          self._content = lowui.Text(self._text)
     39
     40      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     41
     42 -    def selectable(self):
     43          return True
     44
     45      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     46
     47 -    def rows(self, (maxcol,), focus=False):
     48          return len(self._content.get_line_translation(maxcol))
     49
     50      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     51
     52 -    def render(self, (maxcol,), focus=False):
     53          if focus:
     54              self._content.set_text((self._sel_attr, self._text))
     55          else:
     56              if self._entry_attr is not None:
     57                  self._content.set_text((self._entry_attr, self._text))
     58              else:
     59                  self._content.set_text(self._text)
     60
     61          return self._content.render((maxcol,), focus)
     62
     63      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     64
     65 -    def keypress(self, (maxcol,), key):
     66          return key
     67
     68      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     69
     70      text = property(lambda self: self._text)
     71
     72  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     73
     74 -class NumEntry(ListEntry):
     75      """
     76      Entry in list box which can be activated by pressing corresponding number
     77      """
     78
     79 -    def __init__(self, msg, selected_attr, num_order, entry_attr=None,
     80                   enter_cb=None):
     81          """
     82          @param msg: Message
     83          @param selected_attr: Atrribute of selected entry
     84          @param entry_attr: Entry text attribute
     85          @param num_order: Entry number
     86          @param enter_cb: Callback to be executed upon ENTER pressed
     87          """
     88
     89          self._num = []
     90
     91          if callable(enter_cb):
     92              self._enter_cb = enter_cb
     93          else:
     94              self._enter_cb = None
     95
     96          self.num_order = num_order
     97          self._keys = libxyz.ui.Keys()
     98          _msg = u"%d: %s" % (num_order, msg)
     99
    100          super(NumEntry, self).__init__(_msg, selected_attr, entry_attr)
    101
    102      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    103
    104 -    def keypress(self, (maxcol,), key):
    105          if key == self._keys.ENTER and callable(self._enter_cb):
    106              if self._num:
    107                  _index = int("".join(self._num))
    108                  self._num = []
    109              else:
    110                  _index = self.num_order
    111              try:
    112                  self._enter_cb(_index)
    113              except Exception, e:
    114                  xyzlog.error(_(u"Error in entry callback: %s" %
    115                             ustring(str(e))))
    116                  xyzlog.debug(ustring(traceback.format_exc()))
    117
    118              return key
    119          elif key.isdigit():
    120              self._num.append(key)
    121          else:
    122              return key
    123