2009-07-29 14:42:43 +02:00
|
|
|
# Copyright (C) 2009, Aleksey Lim
|
|
|
|
#
|
2009-07-13 02:32:26 +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.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2011-10-29 10:44:18 +02:00
|
|
|
from sugar3.graphics.toolbutton import ToolButton
|
|
|
|
from sugar3.graphics.palette import Palette
|
2009-07-13 05:18:24 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-07-30 13:29:52 +02:00
|
|
|
class RadioMenuButton(ToolButton):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-07-13 02:32:26 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
ToolButton.__init__(self, **kwargs)
|
2009-07-29 17:33:02 +02:00
|
|
|
self.selected_button = None
|
2009-07-13 02:32:26 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2009-07-31 21:56:43 +02:00
|
|
|
def do_clicked(self):
|
|
|
|
if self.palette is None:
|
2009-07-13 02:32:26 +02:00
|
|
|
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)
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-07-30 13:29:52 +02:00
|
|
|
class RadioToolsButton(RadioMenuButton):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-07-13 02:32:26 +02:00
|
|
|
def __init__(self, **kwargs):
|
2009-07-30 13:29:52 +02:00
|
|
|
RadioMenuButton.__init__(self, **kwargs)
|
2009-07-13 02:32:26 +02:00
|
|
|
|
2009-07-31 21:56:43 +02:00
|
|
|
def do_clicked(self):
|
2009-07-29 17:33:02 +02:00
|
|
|
if not self.selected_button:
|
2009-07-13 02:32:26 +02:00
|
|
|
return
|
2009-07-29 17:33:02 +02:00
|
|
|
self.selected_button.emit('clicked')
|
2009-07-13 02:32:26 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-07-13 02:32:26 +02:00
|
|
|
class RadioPalette(Palette):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-07-13 02:32:26 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
Palette.__init__(self, **kwargs)
|
|
|
|
|
2009-07-30 13:29:52 +02:00
|
|
|
self.button_box = gtk.HBox()
|
|
|
|
self.button_box.show()
|
|
|
|
self.set_content(self.button_box)
|
2009-07-13 02:32:26 +02:00
|
|
|
|
2009-07-29 18:15:38 +02:00
|
|
|
def append(self, button, label):
|
2009-07-30 13:29:52 +02:00
|
|
|
children = self.button_box.get_children()
|
2009-07-29 17:33:02 +02:00
|
|
|
|
2009-07-30 13:29:52 +02:00
|
|
|
if button.palette is not None:
|
|
|
|
raise RuntimeError("Palette's button should not have sub-palettes")
|
2009-07-29 18:15:38 +02:00
|
|
|
|
2009-07-13 02:32:26 +02:00
|
|
|
button.show()
|
2009-07-29 17:33:02 +02:00
|
|
|
button.connect('clicked', self.__clicked_cb)
|
2009-07-30 13:29:52 +02:00
|
|
|
self.button_box.pack_start(button, fill=False)
|
2009-07-31 05:49:05 +02:00
|
|
|
button.palette_label = label
|
2009-07-13 02:32:26 +02:00
|
|
|
|
|
|
|
if not children:
|
2009-08-01 15:35:18 +02:00
|
|
|
self.__clicked_cb(button)
|
2009-07-13 03:40:23 +02:00
|
|
|
|
2009-07-13 02:32:26 +02:00
|
|
|
def update_button(self):
|
2009-07-30 13:29:52 +02:00
|
|
|
for i in self.button_box.get_children():
|
2009-08-01 15:35:18 +02:00
|
|
|
self.__clicked_cb(i)
|
2009-07-13 02:32:26 +02:00
|
|
|
|
2009-08-01 15:35:18 +02:00
|
|
|
def __clicked_cb(self, button):
|
2009-07-13 02:32:26 +02:00
|
|
|
if not button.get_active():
|
|
|
|
return
|
|
|
|
|
2009-07-31 05:49:05 +02:00
|
|
|
self.set_primary_text(button.palette_label)
|
2009-08-01 15:35:18 +02:00
|
|
|
self.popdown(immediate=True)
|
2009-07-13 02:32:26 +02:00
|
|
|
|
2009-07-30 13:29:52 +02:00
|
|
|
if self.invoker is not None:
|
|
|
|
parent = self.invoker.parent
|
|
|
|
else:
|
|
|
|
parent = None
|
|
|
|
if not isinstance(parent, RadioMenuButton):
|
2009-07-13 02:32:26 +02:00
|
|
|
return
|
|
|
|
|
2009-08-06 20:40:44 +02:00
|
|
|
parent.props.label = button.palette_label
|
2009-07-29 17:33:02 +02:00
|
|
|
parent.set_icon(button.props.icon_name)
|
|
|
|
parent.selected_button = button
|