merge
[openerp-client.git] / bin / widget_search / selection.py
blob2ee3f80df9fbdbb266cc498394331d59ed009389
1 ##############################################################################
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 # $Id$
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ##############################################################################
30 import gtk
31 import gobject
32 import wid_int
34 class selection(wid_int.wid_int):
35 def __init__(self, name, parent, attrs={}):
36 wid_int.wid_int.__init__(self, name, parent, attrs)
38 self.widget = gtk.combo_box_entry_new_text()
39 self.widget.child.set_editable(True)
40 self.widget.child.connect('key_press_event', self.sig_key_press)
41 self._selection={}
42 if 'selection' in attrs:
43 self.set_popdown(attrs.get('selection',[]))
45 def set_popdown(self, selection):
46 self.model = self.widget.get_model()
47 self.model.clear()
48 self._selection={}
49 lst = []
50 for (i,j) in selection:
51 name = str(j)
52 if type(i)==type(1):
53 name+=' ('+str(i)+')'
54 lst.append(name)
55 self._selection[name]=i
56 self.widget.append_text('')
57 for l in lst:
58 self.widget.append_text(l)
59 return lst
61 def sig_key_press(self, widget, event):
62 completion=gtk.EntryCompletion()
63 completion.set_inline_selection(True)
64 if (event.type == gtk.gdk.KEY_PRESS) \
65 and ((event.state & gtk.gdk.CONTROL_MASK) != 0) \
66 and (event.keyval == gtk.keysyms.space):
67 self.entry.popup()
68 elif not (event.keyval==65362 or event.keyval==65364):
69 completion.set_model(self.model)
70 widget.set_completion(completion)
71 completion.set_text_column(0)
73 def _value_get(self):
74 model = self.widget.get_model()
75 index = self.widget.get_active()
76 if index>=0:
77 res = self._selection.get(model[index][0], False)
78 if res:
79 return [(self.name,'=',res)]
80 return []
82 def _value_set(self, value):
83 if value==False:
84 value=''
85 for s in self._selection:
86 if self._selection[s]==value:
87 self.widget.child.set_text(s)
89 def clear(self):
90 self.widget.child.set_text('')
92 value = property(_value_get, _value_set, None,
93 'The content of the widget or ValueError if not valid')
95 def _readonly_set(self, value):
96 self.widget.set_sensitive(not value)