Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/item.py
blob: e20ab9392c7290646270b512ef266c4f0f851540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (C) 2012-2013 S. Daniel Francis <francis@sugarlabs.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

import logging
logger = logging.getLogger('option')

import gobject
import gtk
from sugar.graphics.toolbutton import ToolButton

import stock


class Item(gobject.GObject):
    __gsignals__ = {'activate': (gobject.SIGNAL_RUN_LAST,
                                 gobject.TYPE_NONE,
                                 tuple())}
    toolitem = None

    def __init__(self, stock_id=gtk.STOCK_CLEAR, important=False):
        gobject.GObject.__init__(self)
        self._stock_id = stock_id
        self.accel_group = None
        self.important = important
        self.connection = None
        self.connection_data = None
        self.tooltip = None

    def set_stock_id(self, stock_id):
        self._stock_id = stock_id

    def get_stock_id(self):
        return self._stock_id

    stock_id = property(get_stock_id, set_stock_id)

    def get_menu_item(self):
        return None

    def activate_cb(self, widget):
        self.emit('activate')

    def setup_accelerator(self):
        accelerator = stock.get_accelerator(self._stock_id)
        logger.debug(str(accelerator))
        try:
            if accelerator[1] > 0:
                self.toolitem.props.accelerator = gtk.accelerator_name(
                    accelerator[1], accelerator[0])
        except:
            logger.error(
'Could not set up accelerator; if toogletoolbutton, update your sugar version')

    def get_tool_item(self):
        sensitive = self.sensitive
        self.toolitem = ToolButton()
        self.setup_tooltip()
        self.setup_tool_icon()
        self.toolitem.connect('clicked', self.activate_cb)
        self.toolitem.set_sensitive(sensitive)
        return self.toolitem

    def setup_tool_icon(self):
        mstyle = self.toolitem.get_style()
        iconset = stock.stock_items[self.stock_id]['has_icon']
        if iconset is not None:
            try:
                pixbuf = iconset.render_icon(mstyle, gtk.TEXT_DIR_NONE,
                                             gtk.STATE_NORMAL,
                                             gtk.ICON_SIZE_LARGE_TOOLBAR)
                image = gtk.image_new_from_pixbuf(pixbuf)
                image.show()
                self.toolitem.set_icon_widget(image)
            except Exception, err:
                logger.debug(self.stock_id)
                logger.error(err)

    def setup_tooltip(self):
        if self.tooltip:
            self.toolitem.set_tooltip(self.tooltip)
        else:
            self.toolitem.set_tooltip(
                stock.get_label(self._stock_id))
        self.setup_accelerator()

    def set_sensitive(self, setting):
        if self.toolitem:
            self.toolitem.set_sensitive(setting)

    def get_sensitive(self):
        if self.toolitem:
            return self.toolitem.get_sensitive()
        else:
            return True

    sensitive = property(get_sensitive, set_sensitive)