Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/graphics/radiopalette.py
blob: b76ae4c47948986b9de8967f62ed484701696eb7 (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
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright (C) 2009, Aleksey Lim
#
# 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.
#
# This library 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
# Lesser General Public License for more details.
#
# 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.

import gtk
import gobject
import logging
from gobject import SIGNAL_RUN_FIRST, TYPE_NONE, TYPE_PYOBJECT

from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.palette import Palette

class RadioPaletteButton(ToolButton):
    def __init__(self, **kwargs):
        ToolButton.__init__(self, **kwargs)
        self.selected_button = None

        if self.props.palette:
            self.__palette_cb(None, None)

        self.connect('notify::palette', self.__palette_cb)

    def __palette_cb(self, widget, pspec):
        if not isinstance(self.props.palette, RadioPalette):
            return
        self.props.palette.update_button()

class RadioMenuButton(RadioPaletteButton):
    def __init__(self, **kwargs):
        RadioPaletteButton.__init__(self, **kwargs)

    def do_clicked(self):
        if not self.palette:
            return
        if self.palette.is_up() and \
                self.palette.palette_state == Palette.SECONDARY:
            self.palette.popdown(immediate=True)
        else:
            self.palette.popup(immediate=True, state=Palette.SECONDARY)

    def do_expose_event(self, event):
        ToolButton.do_expose_event(self, event)
        if not self.palette:
            return

        if self.palette.is_up():
            type = gtk.ARROW_DOWN
        else:
            type = gtk.ARROW_UP

        a = self.allocation
        self.get_style().paint_arrow(event.window,
                gtk.STATE_NORMAL, gtk.SHADOW_IN, event.area, self,
                None, type,  True,
                a.x + a.width/2 - style.TOOLBAR_ARROW_SIZE/2,
                a.y + a.height - style.TOOLBAR_ARROW_SIZE - \
                        style._FOCUS_LINE_WIDTH,
                style.TOOLBAR_ARROW_SIZE, style.TOOLBAR_ARROW_SIZE)

class RadioToolsButton(RadioPaletteButton):
    def __init__(self, **kwargs):
        RadioPaletteButton.__init__(self, **kwargs)

    def do_clicked(self):
        if not self.selected_button:
            return
        self.selected_button.emit('clicked')

class RadioPalette(Palette):
    def __init__(self, **kwargs):
        Palette.__init__(self, **kwargs)

        self.top = gtk.HBox()
        self.top.show()
        self.set_content(self.top)

    def append(self, button, label):
        children = self.top.get_children()

        # palette's button should not have sub-palettes
        button.palette = None

        button.show()
        button.connect('clicked', self.__clicked_cb)
        self.top.pack_start(button, fill=False)
        button.__palette_label = label

        if not children:
            self.__clicked_cb(button, True)

    def update_button(self):
        for i in self.top.get_children():
            self.__clicked_cb(i, True)

    def __clicked_cb(self, button, quiet=False):
        if not button.get_active():
            return

        self.set_primary_text(button.__palette_label)
        if not quiet:
            self.popdown(immediate=True)

        parent = self.invoker and self.invoker.parent
        if not isinstance(parent, RadioPaletteButton):
            return

        parent.set_icon(button.props.icon_name)
        parent.selected_button = button