1 ##############################################################################
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
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
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 ##############################################################################
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
])
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
)
65 for (value
, name
) in selection
:
68 self
._selection
[name
] = value
71 self
.entry
.set_model(model
)
72 self
.entry
.set_text_column(0)
75 def _readonly_set(self
, value
):
76 interface
.widget_interface
._readonly
_set
(self
, value
)
77 self
.entry
.set_sensitive(not value
)
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
):
90 def sig_activate(self
, *args
):
91 text
= self
.entry
.child
.get_text()
94 for txt
, val
in self
._selection
.items():
97 if txt
[:len(text
)].lower() == text
.lower():
99 if len(txt
) == len(text
):
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
):
115 self
.entry
.child
.set_text('')
118 super(selection
, self
).display(model
, model_field
)
119 value
= model_field
.get(model
)
121 self
.entry
.child
.set_text('')
124 for long_text
, sel_value
in self
._selection
.items():
125 if sel_value
== value
:
126 self
.entry
.child
.set_text(long_text
)
131 def sig_changed(self
, *args
):
135 def _color_widget(self
):
136 return self
.entry
.child