2007-04-29 21:10:58 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
|
|
|
#
|
2007-06-13 12:21:49 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
2007-04-29 21:10:58 +02:00
|
|
|
#
|
2007-06-13 12:21:49 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2007-04-29 21:10:58 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-06-13 12:21:49 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2007-04-29 21:10:58 +02:00
|
|
|
#
|
2007-06-13 12:21:49 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
2007-04-29 21:10:58 +02:00
|
|
|
import sys
|
2007-06-25 15:33:10 +02:00
|
|
|
import os
|
2007-05-05 12:13:23 +02:00
|
|
|
import logging
|
2007-04-29 21:10:58 +02:00
|
|
|
|
|
|
|
import gobject
|
|
|
|
import gtk
|
|
|
|
|
|
|
|
class ComboBox(gtk.ComboBox):
|
|
|
|
__gtype_name__ = 'SugarComboBox'
|
|
|
|
|
|
|
|
__gproperties__ = {
|
2007-06-25 15:33:10 +02:00
|
|
|
'value' : (object, None, None,
|
2007-04-29 21:10:58 +02:00
|
|
|
gobject.PARAM_READABLE)
|
|
|
|
}
|
|
|
|
def __init__(self):
|
|
|
|
gtk.ComboBox.__init__(self)
|
|
|
|
|
2007-05-05 12:13:23 +02:00
|
|
|
self._text_renderer = None
|
|
|
|
self._icon_renderer = None
|
|
|
|
|
2007-06-25 15:33:10 +02:00
|
|
|
self._model = gtk.ListStore(gobject.TYPE_PYOBJECT,
|
2007-04-29 21:10:58 +02:00
|
|
|
gobject.TYPE_STRING,
|
2007-06-25 15:33:10 +02:00
|
|
|
gtk.gdk.Pixbuf,
|
2007-04-29 21:10:58 +02:00
|
|
|
gobject.TYPE_BOOLEAN)
|
|
|
|
self.set_model(self._model)
|
|
|
|
|
|
|
|
self.set_row_separator_func(self._is_separator)
|
|
|
|
|
|
|
|
def do_get_property(self, pspec):
|
|
|
|
if pspec.name == 'value':
|
2007-07-03 17:08:36 +02:00
|
|
|
row = self.get_active_item()
|
|
|
|
if not row:
|
|
|
|
return None
|
|
|
|
return row[0]
|
2007-04-29 21:10:58 +02:00
|
|
|
else:
|
|
|
|
return gtk.ComboBox.do_get_property(self, pspec)
|
|
|
|
|
2007-07-23 13:15:02 +02:00
|
|
|
def _get_real_name_from_theme(self, name, size):
|
2007-07-03 17:08:36 +02:00
|
|
|
icon_theme = gtk.icon_theme_get_default()
|
2007-07-23 13:15:02 +02:00
|
|
|
width, height = gtk.icon_size_lookup(size)
|
2007-07-23 23:27:11 +02:00
|
|
|
info = icon_theme.lookup_icon(name, width, 0)
|
2007-07-03 17:08:36 +02:00
|
|
|
if not info:
|
|
|
|
raise ValueError("Icon '" + name + "' not found.")
|
|
|
|
fname = info.get_filename()
|
|
|
|
del info
|
|
|
|
return fname
|
|
|
|
|
2007-08-26 20:44:51 +02:00
|
|
|
def append_item(self, action_id, text, icon_name=None, file_name=None):
|
|
|
|
if not self._icon_renderer and (icon_name or file_name):
|
2007-05-05 12:13:23 +02:00
|
|
|
self._icon_renderer = gtk.CellRendererPixbuf()
|
2007-07-31 14:56:05 +02:00
|
|
|
|
|
|
|
settings = self.get_settings()
|
|
|
|
w, h = gtk.icon_size_lookup_for_settings(settings, gtk.ICON_SIZE_MENU)
|
|
|
|
self._icon_renderer.props.stock_size = w
|
|
|
|
|
2007-05-05 12:13:23 +02:00
|
|
|
self.pack_start(self._icon_renderer, False)
|
2007-06-25 15:33:10 +02:00
|
|
|
self.add_attribute(self._icon_renderer, 'pixbuf', 2)
|
2007-05-05 12:13:23 +02:00
|
|
|
|
|
|
|
if not self._text_renderer and text:
|
|
|
|
self._text_renderer = gtk.CellRendererText()
|
|
|
|
self.pack_end(self._text_renderer, True)
|
|
|
|
self.add_attribute(self._text_renderer, 'text', 1)
|
|
|
|
|
2007-08-26 20:44:51 +02:00
|
|
|
if icon_name or file_name:
|
2007-07-23 13:15:02 +02:00
|
|
|
if text:
|
|
|
|
size = gtk.ICON_SIZE_MENU
|
|
|
|
else:
|
|
|
|
size = gtk.ICON_SIZE_LARGE_TOOLBAR
|
|
|
|
width, height = gtk.icon_size_lookup(size)
|
2007-08-26 20:44:51 +02:00
|
|
|
|
|
|
|
if icon_name:
|
2007-08-27 13:17:50 +02:00
|
|
|
file_name = self._get_real_name_from_theme(icon_name, size)
|
2007-08-26 20:44:51 +02:00
|
|
|
|
|
|
|
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(file_name, width, height)
|
2007-06-25 15:33:10 +02:00
|
|
|
else:
|
|
|
|
pixbuf = None
|
|
|
|
|
|
|
|
self._model.append([action_id, text, pixbuf, False])
|
2007-04-29 21:10:58 +02:00
|
|
|
|
|
|
|
def append_separator(self):
|
|
|
|
self._model.append([0, None, None, True])
|
|
|
|
|
|
|
|
def get_active_item(self):
|
|
|
|
index = self.get_active()
|
|
|
|
if index == -1:
|
|
|
|
index = 0
|
|
|
|
|
|
|
|
row = self._model.iter_nth_child(None, index)
|
2007-07-03 17:08:36 +02:00
|
|
|
if not row:
|
|
|
|
return None
|
2007-04-29 21:10:58 +02:00
|
|
|
return self._model[row]
|
|
|
|
|
2007-07-03 17:08:36 +02:00
|
|
|
def remove_all(self):
|
|
|
|
self._model.clear()
|
|
|
|
|
2007-04-29 21:10:58 +02:00
|
|
|
def _is_separator(self, model, row):
|
|
|
|
action_id, text, icon_name, is_separator = model[row]
|
|
|
|
return is_separator
|