Mercurial > fife-parpg
comparison engine/python/fife/extensions/pychan/widgets/dropdown.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 from listbox import GenericListmodel | |
27 | |
28 class DropDown(Widget): | |
29 """ | |
30 A dropdown or combo box widget for selecting lists of strings. | |
31 | |
32 New Attributes | |
33 ============== | |
34 | |
35 - items: A List of strings. This can be treated like an ordinary python list. | |
36 but only strings are allowed. | |
37 - selected: The index of the selected item in the list. Starting from C{0} to C{len(items)-1}. | |
38 A negative value indicates, that no item is selected. | |
39 - selected_item: The selected string itself, or C{None} - if no string is selected. | |
40 | |
41 Data | |
42 ==== | |
43 The selected attribute can be read and set via L{distributeData} and L{collectData}. | |
44 The list items can be set via L{distributeInitialData}. | |
45 """ | |
46 def __init__(self,items=[],**kwargs): | |
47 self._items = GenericListmodel(*items) | |
48 self.real_widget = fife.DropDown(self._items) | |
49 super(DropDown,self).__init__(**kwargs) | |
50 | |
51 # Prepare Data collection framework | |
52 self.accepts_initial_data = True | |
53 self._realSetInitialData = self._setItems | |
54 | |
55 self.accepts_data = True | |
56 self._realSetData = self._setSelected | |
57 self._realGetData = self._getSelected | |
58 | |
59 def resizeToContent(self,recurse=True): | |
60 # We append a minimum value, so max() does not bail out, | |
61 # if no items are in the list | |
62 _item_widths = map(self.real_font.getWidth, map(text2gui, map(unicode, self._items))) + [self.real_font.getHeight()] | |
63 max_w = max(_item_widths) | |
64 self.width = max_w | |
65 self.height = (self.real_font.getHeight() + 2) | |
66 | |
67 def _getItems(self): return self._items | |
68 def _setItems(self,items): | |
69 # Note we cannot use real_widget.setListModel | |
70 # for some reason ??? | |
71 | |
72 # Also self assignment can kill you | |
73 if id(items) != id(self._items): | |
74 self._items.clear() | |
75 self._items.extend(items) | |
76 items = property(_getItems,_setItems) | |
77 | |
78 def _getSelected(self): return self.real_widget.getSelected() | |
79 def _setSelected(self,index): self.real_widget.setSelected(index) | |
80 selected = property(_getSelected,_setSelected) | |
81 def _getSelectedItem(self): | |
82 if 0 <= self.selected < len(self._items): | |
83 return self._items[self.selected] | |
84 return None | |
85 selected_item = property(_getSelectedItem) |