comparison engine/python/fife/extensions/pychan/widgets/listbox.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 from common import *
25 from widget import Widget
26
27 class GenericListmodel(fife.ListModel,list):
28 """
29 A wrapper for the exported list model to behave more like a Python list.
30 Don't use directly.
31 """
32 def __init__(self,*args):
33 super(GenericListmodel,self).__init__()
34 map(self.append,args)
35 def clear(self):
36 while len(self):
37 self.pop()
38 def getNumberOfElements(self):
39 return len(self)
40
41 def getElementAt(self, i):
42 i = max(0,min(i,len(self) - 1))
43 return text2gui(unicode(self[i]))
44
45 class ListBox(Widget):
46 """
47 A basic list box widget for displaying lists of strings. It makes most sense to wrap
48 this into a L{ScrollArea}.
49
50 New Attributes
51 ==============
52
53 - items: A List of strings. This can be treated like an ordinary python list.
54 but only strings are allowed.
55 - selected: The index of the selected item in the list. Starting from C{0} to C{len(items)-1}.
56 A negative value indicates, that no item is selected.
57 - selected_item: The selected string itself, or C{None} - if no string is selected.
58
59 Data
60 ====
61 The selected attribute can be read and set via L{distributeData} and L{collectData}.
62 The list items can be set via L{distributeInitialData}.
63 """
64 DEFAULT_HEXPAND = 1
65 DEFAULT_VEXPAND = 1
66
67 def __init__(self,items=[],**kwargs):
68 self._items = GenericListmodel(*items)
69 self.real_widget = fife.ListBox(self._items)
70 super(ListBox,self).__init__(**kwargs)
71
72 # Prepare Data collection framework
73 self.accepts_initial_data = True
74 self._realSetInitialData = self._setItems
75
76 self.accepts_data = True
77 self._realSetData = self._setSelected
78 self._realGetData = self._getSelected
79
80 def resizeToContent(self,recurse=True):
81 # We append a minimum value, so max() does not bail out,
82 # if no items are in the list
83 _item_widths = map(self.real_font.getWidth,map(gui2str,self._items)) + [0]
84 max_w = max(_item_widths)
85 self.width = max_w
86 self.height = (self.real_font.getHeight() + 2) * len(self._items)
87
88 def _getItems(self): return self._items
89 def _setItems(self,items):
90 # Note we cannot use real_widget.setListModel
91 # for some reason ???
92
93 # Also self assignment can kill you
94 if id(items) != id(self._items):
95 self._items.clear()
96 self._items.extend(items)
97
98 items = property(_getItems,_setItems)
99
100 def _getSelected(self): return self.real_widget.getSelected()
101 def _setSelected(self,index): self.real_widget.setSelected(index)
102 selected = property(_getSelected,_setSelected)
103 def _getSelectedItem(self):
104 if 0 <= self.selected < len(self._items):
105 return self._items[self.selected]
106 return None
107 selected_item = property(_getSelectedItem)