1
# Copyright (C) 2007, One Laptop Per Child
3
# This library is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU Lesser General Public
5
# License as published by the Free Software Foundation; either
6
# version 2 of the License, or (at your option) any later version.
8
# This library is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# Lesser General Public License for more details.
13
# You should have received a copy of the GNU Lesser General Public
14
# License along with this library; if not, write to the
15
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16
# Boston, MA 02111-1307, USA.
21
class ComboBox(gtk.ComboBox):
22
__gtype_name__ = 'SugarComboBox'
25
'value' : (object, None, None,
26
gobject.PARAM_READABLE)
29
gtk.ComboBox.__init__(self)
31
self._text_renderer = None
32
self._icon_renderer = None
34
self._model = gtk.ListStore(gobject.TYPE_PYOBJECT,
38
self.set_model(self._model)
40
self.set_row_separator_func(self._is_separator)
42
def do_get_property(self, pspec):
43
if pspec.name == 'value':
44
row = self.get_active_item()
49
return gtk.ComboBox.do_get_property(self, pspec)
51
def _get_real_name_from_theme(self, name, size):
52
icon_theme = gtk.icon_theme_get_default()
53
width, height = gtk.icon_size_lookup(size)
54
info = icon_theme.lookup_icon(name, max(width, height), 0)
56
raise ValueError("Icon '" + name + "' not found.")
57
fname = info.get_filename()
61
def append_item(self, action_id, text, icon_name=None, file_name=None):
62
if not self._icon_renderer and (icon_name or file_name):
63
self._icon_renderer = gtk.CellRendererPixbuf()
65
settings = self.get_settings()
66
w, h = gtk.icon_size_lookup_for_settings(
67
settings, gtk.ICON_SIZE_MENU)
68
self._icon_renderer.props.stock_size = max(w, h)
70
self.pack_start(self._icon_renderer, False)
71
self.add_attribute(self._icon_renderer, 'pixbuf', 2)
73
if not self._text_renderer and text:
74
self._text_renderer = gtk.CellRendererText()
75
self.pack_end(self._text_renderer, True)
76
self.add_attribute(self._text_renderer, 'text', 1)
78
if icon_name or file_name:
80
size = gtk.ICON_SIZE_MENU
82
size = gtk.ICON_SIZE_LARGE_TOOLBAR
83
width, height = gtk.icon_size_lookup(size)
86
file_name = self._get_real_name_from_theme(icon_name, size)
88
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
89
file_name, width, height)
93
self._model.append([action_id, text, pixbuf, False])
95
def append_separator(self):
96
self._model.append([0, None, None, True])
98
def get_active_item(self):
99
index = self.get_active()
103
row = self._model.iter_nth_child(None, index)
106
return self._model[row]
108
def remove_all(self):
111
def _is_separator(self, model, row):