sugar-toolkit-gtk3/src/sugar/graphics/radiopalette.py

125 lines
4.0 KiB
Python
Raw Normal View History

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
import gobject
import logging
2009-07-29 17:33:02 +02:00
from gobject import SIGNAL_RUN_FIRST, TYPE_NONE, TYPE_PYOBJECT
2009-07-13 02:32:26 +02:00
from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.palette import Palette
2009-07-13 05:18:24 +02:00
2009-07-30 13:29:52 +02:00
class RadioMenuButton(ToolButton):
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)
self.connect('clicked', self.__clicked_cb)
def __clicked_cb(self, button):
2009-07-31 05:49:05 +02:00
self.on_click()
2009-07-13 02:32:26 +02:00
def __palette_cb(self, widget, pspec):
if not isinstance(self.props.palette, RadioPalette):
return
self.props.palette.update_button()
2009-07-31 05:49:05 +02:00
def on_click(self):
2009-07-13 02:32:26 +02:00
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_UP
2009-07-30 13:29:52 +02:00
else:
type = gtk.ARROW_DOWN
2009-07-13 02:32:26 +02:00
2009-07-30 13:29:52 +02:00
alloc = self.allocation
x = alloc.x + alloc.width / 2 - style.TOOLBAR_ARROW_SIZE / 2
y = alloc.y + alloc.height - int(style.TOOLBAR_ARROW_SIZE * .85)
2009-07-13 02:32:26 +02:00
self.get_style().paint_arrow(event.window,
2009-07-30 13:29:52 +02:00
gtk.STATE_NORMAL, gtk.SHADOW_NONE, event.area, self,
2009-07-13 02:32:26 +02:00
None, type, True,
2009-07-30 13:29:52 +02:00
x, y, style.TOOLBAR_ARROW_SIZE, style.TOOLBAR_ARROW_SIZE)
2009-07-13 02:32:26 +02:00
2009-07-30 13:29:52 +02:00
class RadioToolsButton(RadioMenuButton):
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 05:49:05 +02:00
def on_click(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
class RadioPalette(Palette):
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-07-29 17:33:02 +02:00
self.__clicked_cb(button, True)
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-07-29 17:33:02 +02:00
self.__clicked_cb(i, True)
2009-07-13 02:32:26 +02:00
2009-07-29 17:33:02 +02:00
def __clicked_cb(self, button, quiet=False):
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-07-13 02:32:26 +02:00
if not quiet:
self.popdown(immediate=True)
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-07-29 17:33:02 +02:00
parent.set_icon(button.props.icon_name)
parent.selected_button = button