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 -class NumEntry(ListEntry):
71 """ 72 Entry in list box which can be activated by pressing corresponding number 73 """ 74
75 - def __init__(self, msg, selected_attr, num_order, entry_attr=None, 76 enter_cb=None):
77 """ 78 @param msg: Message 79 @param selected_attr: Atrribute of selected entry 80 @param entry_attr: Entry text attribute 81 @param num_order: Entry number 82 @param enter_cb: Callback to be executed upon ENTER pressed 83 """ 84 85 self._num = [] 86 87 if callable(enter_cb): 88 self._enter_cb = enter_cb 89 else: 90 self._enter_cb = None 91 92 self.num_order = num_order 93 self._keys = libxyz.ui.Keys() 94 _msg = u"%d: %s" % (num_order, msg) 95 96 super(NumEntry, self).__init__(_msg, selected_attr, entry_attr)
97 98 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 99
100 - def keypress(self, (maxcol,), key):
101 if key == self._keys.ENTER and callable(self._enter_cb): 102 if self._num: 103 _index = int("".join(self._num)) 104 self._num = [] 105 else: 106 _index = self.num_order 107 try: 108 self._enter_cb(_index) 109 except Exception, e: 110 xyzlog.error(_(u"Error in entry callback: %s" % 111 ustring(str(e)))) 112 xyzlog.debug(ustring(traceback.format_exc())) 113 114 return key 115 elif key.isdigit(): 116 self._num.append(key) 117 else: 118 return key
119