use keysyms instead of magic numbers
[openerp-client.git] / bin / widget / view / form_gtk / selection.py
blob2e1e703cc07a5264403a8391b9671ee7d77534c3
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
6 # $Id$
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ##############################################################################
31 import common
32 import interface
33 import gtk
34 import gobject
36 import gettext
39 class selection(interface.widget_interface):
40 def __init__(self, window, parent, model, attrs={}):
41 interface.widget_interface.__init__(self, window, parent, model, attrs)
43 self.widget = gtk.HBox(spacing=3)
44 self.entry = gtk.ComboBoxEntry()
45 self.entry.child.set_property('activates_default', True)
46 self.entry.child.connect('changed', self.sig_changed)
47 self.entry.child.connect('button_press_event', self._menu_open)
48 self.entry.child.connect('key_press_event', self.sig_key_press)
49 self.entry.child.connect('activate', self.sig_activate)
50 self.entry.child.connect_after('focus-out-event', self.sig_activate)
51 self.entry.set_size_request(int(attrs.get('size', -1)), -1)
52 self.widget.pack_start(self.entry, expand=True, fill=True)
54 # the dropdown button is not focusable by a tab
55 self.widget.set_focus_chain([self.entry.child])
57 self.ok = True
58 self._selection={}
59 self.set_popdown(attrs.get('selection',[]))
60 self.last_key = (None, 0)
62 def set_popdown(self, selection):
63 self.model = gtk.ListStore(gobject.TYPE_STRING)
64 self._selection={}
65 lst = []
66 for (value, name) in selection:
67 name = str(name)
68 lst.append(name)
69 self._selection[name] = value
70 i = self.model.append()
71 self.model.set(i, 0, name)
72 self.entry.set_model(self.model)
73 self.entry.set_text_column(0)
74 return lst
76 def _readonly_set(self, value):
77 interface.widget_interface._readonly_set(self, value)
78 self.entry.set_sensitive(not value)
80 def value_get(self):
81 res = self.entry.child.get_text()
82 return self._selection.get(res, False)
84 def sig_key_press(self, widget, event):
85 # allow showing available entries by hitting "ctrl+space"
86 completion=gtk.EntryCompletion()
87 completion.set_inline_selection(True)
88 if (event.type == gtk.gdk.KEY_PRESS) \
89 and ((event.state & gtk.gdk.CONTROL_MASK) != 0) \
90 and (event.keyval == gtk.keysyms.space):
91 self.entry.popup()
92 elif not (event.keyval == gtk.keysyms.Up or event.keyval == gtk.keysyms.Down):
93 completion.set_model(self.model)
94 widget.set_completion(completion)
95 completion.set_text_column(0)
97 def sig_activate(self, *args):
98 text = self.entry.child.get_text()
99 value = False
100 if text:
101 for txt, val in self._selection.items():
102 if not val:
103 continue
104 if txt[:len(text)].lower() == text.lower():
105 value = val
106 if len(txt) == len(text):
107 break
108 self._view.modelfield.set_client(self._view.model, value, force_change=True)
109 self.display(self._view.model, self._view.modelfield)
112 def set_value(self, model, model_field):
113 model_field.set_client(model, self.value_get())
115 def _menu_sig_default_set(self):
116 self.set_value(self._view.model, self._view.modelfield)
117 super(selection, self)._menu_sig_default_set()
119 def display(self, model, model_field):
120 self.ok = False
121 if not model_field:
122 self.entry.child.set_text('')
123 self.ok = True
124 return False
125 super(selection, self).display(model, model_field)
126 value = model_field.get(model)
127 if not value:
128 self.entry.child.set_text('')
129 else:
130 found = False
131 for long_text, sel_value in self._selection.items():
132 if sel_value == value:
133 self.entry.child.set_text(long_text)
134 found = True
135 break
136 self.ok = True
138 def sig_changed(self, *args):
139 if self.ok:
140 self._focus_out()
142 def _color_widget(self):
143 return self.entry.child
145 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: