~ubuntu-branches/ubuntu/karmic/sugar-toolkit/karmic

« back to all changes in this revision

Viewing changes to src/sugar/graphics/combobox.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-08-07 19:43:09 UTC
  • mfrom: (0.1.2 lenny) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080807194309-03302c4lj0j0ipze
Tags: 0.82.0-1
* New upstream release.
* Unfuzz patch 2991.
* Add DEB_MAINTAINER_MODE in debian/rules (thanks to Romain Beauxis).
* Update copyright-hints.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, One Laptop Per Child
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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.
 
17
 
 
18
import gobject
 
19
import gtk
 
20
 
 
21
class ComboBox(gtk.ComboBox):
 
22
    __gtype_name__ = 'SugarComboBox'    
 
23
 
 
24
    __gproperties__ = {
 
25
        'value'    : (object, None, None,
 
26
                      gobject.PARAM_READABLE)
 
27
    }
 
28
    def __init__(self):
 
29
        gtk.ComboBox.__init__(self)
 
30
 
 
31
        self._text_renderer = None
 
32
        self._icon_renderer = None
 
33
 
 
34
        self._model = gtk.ListStore(gobject.TYPE_PYOBJECT,
 
35
                                    gobject.TYPE_STRING,
 
36
                                    gtk.gdk.Pixbuf,
 
37
                                    gobject.TYPE_BOOLEAN)
 
38
        self.set_model(self._model)
 
39
 
 
40
        self.set_row_separator_func(self._is_separator)
 
41
 
 
42
    def do_get_property(self, pspec):
 
43
        if pspec.name == 'value':
 
44
            row = self.get_active_item()
 
45
            if not row:
 
46
                return None
 
47
            return row[0]
 
48
        else:
 
49
            return gtk.ComboBox.do_get_property(self, pspec)
 
50
 
 
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)
 
55
        if not info:
 
56
            raise ValueError("Icon '" + name + "' not found.")
 
57
        fname = info.get_filename()
 
58
        del info
 
59
        return fname
 
60
 
 
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()
 
64
 
 
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)
 
69
 
 
70
            self.pack_start(self._icon_renderer, False)
 
71
            self.add_attribute(self._icon_renderer, 'pixbuf', 2)
 
72
 
 
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)
 
77
 
 
78
        if icon_name or file_name:
 
79
            if text:
 
80
                size = gtk.ICON_SIZE_MENU
 
81
            else:
 
82
                size = gtk.ICON_SIZE_LARGE_TOOLBAR
 
83
            width, height = gtk.icon_size_lookup(size)
 
84
 
 
85
            if icon_name:
 
86
                file_name = self._get_real_name_from_theme(icon_name, size)
 
87
 
 
88
            pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(
 
89
                                                file_name, width, height)
 
90
        else:
 
91
            pixbuf = None
 
92
 
 
93
        self._model.append([action_id, text, pixbuf, False])
 
94
 
 
95
    def append_separator(self):
 
96
        self._model.append([0, None, None, True])    
 
97
 
 
98
    def get_active_item(self):
 
99
        index = self.get_active()
 
100
        if index == -1:
 
101
            index = 0
 
102
 
 
103
        row = self._model.iter_nth_child(None, index)
 
104
        if not row:
 
105
            return None
 
106
        return self._model[row]
 
107
 
 
108
    def remove_all(self):
 
109
        self._model.clear()
 
110
 
 
111
    def _is_separator(self, model, row):
 
112
        return model[row][3]