ergo: selection fields: use keysyms
[openerp-client.git] / bin / widget / view / form_gtk / selection.py
blobed9535a123ad4d61a63f38cd52de5c544460c2b1
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 common
31 import interface
32 import gtk
33 import gobject
35 import gettext
38 class selection(interface.widget_interface):
39 def __init__(self, window, parent, model, attrs={}):
40 interface.widget_interface.__init__(self, window, parent, model, attrs)
42 self.widget = gtk.HBox(spacing=3)
43 self.entry = gtk.ComboBoxEntry()
44 self.entry.child.set_property('activates_default', True)
45 self.entry.child.connect('changed', self.sig_changed)
46 self.entry.child.connect('button_press_event', self._menu_open)
47 self.entry.child.connect('key_press_event', self.sig_key_press)
48 self.entry.child.connect('activate', self.sig_activate)
49 self.entry.child.connect_after('focus-out-event', self.sig_activate)
50 self.entry.set_size_request(int(attrs.get('size', -1)), -1)
51 self.widget.pack_start(self.entry, expand=True, fill=True)
53 # the dropdown button is not focusable by a tab
54 self.widget.set_focus_chain([self.entry.child])
56 self.ok = True
57 self._selection={}
58 self.set_popdown(attrs.get('selection',[]))
59 self.last_key = (None, 0)
61 def set_popdown(self, selection):
62 model = gtk.ListStore(gobject.TYPE_STRING)
63 self._selection={}
64 lst = []
65 for (value, name) in selection:
66 name = str(name)
67 lst.append(name)
68 self._selection[name] = value
69 i = model.append()
70 model.set(i, 0, name)
71 self.entry.set_model(model)
72 self.entry.set_text_column(0)
73 return lst
75 def _readonly_set(self, value):
76 interface.widget_interface._readonly_set(self, value)
77 self.entry.set_sensitive(not value)
79 def value_get(self):
80 res = self.entry.child.get_text()
81 return self._selection.get(res, False)
83 def sig_key_press(self, widget, event):
84 # allow showing available entries by hitting "ctrl+space"
85 if (event.type == gtk.gdk.KEY_PRESS) \
86 and ((event.state & gtk.gdk.CONTROL_MASK) != 0) \
87 and (event.keyval == gtk.keysyms.space):
88 self.entry.popup()
90 def sig_activate(self, *args):
91 text = self.entry.child.get_text()
92 value = False
93 if text:
94 for txt, val in self._selection.items():
95 if not val:
96 continue
97 if txt[:len(text)].lower() == text.lower():
98 value = val
99 if len(txt) == len(text):
100 break
101 self._view.modelfield.set_client(self._view.model, value, force_change=True)
102 self.display(self._view.model, self._view.modelfield)
105 def set_value(self, model, model_field):
106 model_field.set_client(model, self.value_get())
108 def _menu_sig_default_set(self):
109 self.set_value(self._view.model, self._view.modelfield)
110 super(selection, self)._menu_sig_default_set()
112 def display(self, model, model_field):
113 self.ok = False
114 if not model_field:
115 self.entry.child.set_text('')
116 self.ok = True
117 return False
118 super(selection, self).display(model, model_field)
119 value = model_field.get(model)
120 if not value:
121 self.entry.child.set_text('')
122 else:
123 found = False
124 for long_text, sel_value in self._selection.items():
125 if sel_value == value:
126 self.entry.child.set_text(long_text)
127 found = True
128 break
129 self.ok = True
131 def sig_changed(self, *args):
132 if self.ok:
133 self._focus_out()
135 def _color_widget(self):
136 return self.entry.child