2009-07-29 14:42:43 +02:00
|
|
|
# Copyright (C) 2009, Aleksey Lim
|
|
|
|
#
|
2009-07-10 05:58:13 +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 logging
|
2009-08-01 13:23:06 +02:00
|
|
|
|
|
|
|
import gobject
|
2009-07-10 05:58:13 +02:00
|
|
|
from gobject import SIGNAL_RUN_FIRST, TYPE_NONE
|
2009-08-01 13:23:06 +02:00
|
|
|
import gtk
|
2009-07-10 05:58:13 +02:00
|
|
|
|
|
|
|
from sugar.graphics import style
|
2009-08-01 13:23:06 +02:00
|
|
|
from sugar.graphics.palette import PaletteWindow, ToolInvoker
|
2009-07-10 05:58:13 +02:00
|
|
|
from sugar.graphics.toolbutton import ToolButton
|
2009-07-31 22:43:35 +02:00
|
|
|
from sugar.graphics import palettegroup
|
2009-07-10 05:58:13 +02:00
|
|
|
|
|
|
|
class ToolbarButton(ToolButton):
|
2009-08-01 13:23:06 +02:00
|
|
|
def __init__(self, page=None, **kwargs):
|
2009-07-12 03:42:23 +02:00
|
|
|
ToolButton.__init__(self, **kwargs)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-08-01 13:23:06 +02:00
|
|
|
self.set_page(page)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
|
|
|
self.connect('clicked',
|
2009-07-31 09:10:17 +02:00
|
|
|
lambda widget: self.set_expanded(not self.is_expanded()))
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-07-31 05:49:05 +02:00
|
|
|
def get_toolbar_box(self):
|
2009-07-12 03:42:23 +02:00
|
|
|
if not hasattr(self.parent, 'owner'):
|
|
|
|
return None
|
|
|
|
return self.parent.owner
|
|
|
|
|
2009-07-31 05:49:05 +02:00
|
|
|
toolbar_box = property(get_toolbar_box)
|
2009-07-12 03:42:23 +02:00
|
|
|
|
|
|
|
def get_page(self):
|
2009-07-31 09:10:17 +02:00
|
|
|
if self.page_widget is None:
|
|
|
|
return None
|
|
|
|
return self.page_widget.child.child
|
2009-07-12 03:42:23 +02:00
|
|
|
|
|
|
|
def set_page(self, page):
|
2009-08-01 13:23:06 +02:00
|
|
|
if page is None:
|
|
|
|
self.page_widget = None
|
|
|
|
return
|
2009-07-31 09:10:17 +02:00
|
|
|
self.page_widget = _embody_page(_Box, page)
|
|
|
|
self.page_widget.toolbar_button = self
|
2009-07-12 03:42:23 +02:00
|
|
|
page.show()
|
2009-08-01 13:23:06 +02:00
|
|
|
if self.props.palette is None:
|
|
|
|
self.props.palette = _ToolbarPalette(invoker=ToolInvoker(self))
|
|
|
|
self.props.palette.toolbar_button = self
|
|
|
|
self._move_page_to_palette()
|
2009-07-12 03:42:23 +02:00
|
|
|
|
|
|
|
page = gobject.property(type=object, getter=get_page, setter=set_page)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-08-01 13:23:06 +02:00
|
|
|
def _move_page_to_palette(self):
|
|
|
|
if self.page_widget is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.toolbar_box is not None and \
|
|
|
|
self.page_widget in self.toolbar_box.get_children():
|
|
|
|
self.toolbar_box.remove(self.page_widget)
|
|
|
|
|
2009-07-31 22:43:35 +02:00
|
|
|
if isinstance(self.props.palette, _ToolbarPalette):
|
2009-08-01 13:23:06 +02:00
|
|
|
self.props.palette.add(self.page_widget)
|
|
|
|
|
2009-07-31 09:10:17 +02:00
|
|
|
def is_expanded(self):
|
|
|
|
return self.toolbar_box is not None and self.page_widget is not None \
|
|
|
|
and self.toolbar_box.expanded_button == self
|
2009-07-12 03:42:23 +02:00
|
|
|
|
2009-07-31 09:10:17 +02:00
|
|
|
def popdown(self):
|
2009-08-01 13:23:06 +02:00
|
|
|
self.props.palette.popdown(immediate=True)
|
2009-07-12 03:42:23 +02:00
|
|
|
|
2009-07-31 09:10:17 +02:00
|
|
|
def set_expanded(self, expanded):
|
|
|
|
self.popdown()
|
|
|
|
|
|
|
|
box = self.toolbar_box
|
2009-07-12 03:42:23 +02:00
|
|
|
|
2009-07-31 12:55:58 +02:00
|
|
|
if box is None or self.page_widget is None or \
|
|
|
|
self.is_expanded() == expanded:
|
2009-07-31 09:10:17 +02:00
|
|
|
return
|
2009-07-12 03:42:23 +02:00
|
|
|
|
2009-07-31 09:10:17 +02:00
|
|
|
if not expanded:
|
|
|
|
box.remove(self.page_widget)
|
|
|
|
box.expanded_button = None
|
2009-08-01 13:23:06 +02:00
|
|
|
self._move_page_to_palette()
|
2009-07-31 09:10:17 +02:00
|
|
|
return
|
2009-07-12 03:42:23 +02:00
|
|
|
|
2009-07-31 09:10:17 +02:00
|
|
|
if box.expanded_button is not None:
|
|
|
|
expanded_toolitem = box.expanded_button.page_widget.toolbar_button
|
2009-07-31 12:55:58 +02:00
|
|
|
if expanded_toolitem.window is not None:
|
2009-07-31 09:10:17 +02:00
|
|
|
expanded_toolitem.window.invalidate_rect(None, True)
|
|
|
|
box.expanded_button.set_expanded(False)
|
2009-07-12 03:42:23 +02:00
|
|
|
|
2009-07-31 12:55:58 +02:00
|
|
|
if self.page_widget.parent is not None:
|
2009-08-01 13:23:06 +02:00
|
|
|
self.props.palette.remove(self.page_widget)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-07-31 09:10:17 +02:00
|
|
|
self.modify_bg(gtk.STATE_NORMAL, box.background)
|
|
|
|
_setup_page(self.page_widget, box.background, box.props.padding)
|
|
|
|
box.pack_start(self.page_widget)
|
|
|
|
box.expanded_button = self
|
2009-07-10 05:58:13 +02:00
|
|
|
|
|
|
|
def do_expose_event(self, event):
|
2009-08-01 13:23:06 +02:00
|
|
|
if not self.is_expanded() or self.props.palette is not None and \
|
|
|
|
self.props.palette.is_up():
|
2009-07-10 05:58:13 +02:00
|
|
|
ToolButton.do_expose_event(self, event)
|
2009-07-31 05:58:52 +02:00
|
|
|
_paint_arrow(self, event, gtk.ARROW_DOWN)
|
2009-07-10 05:58:13 +02:00
|
|
|
return
|
|
|
|
|
2009-07-12 06:32:51 +02:00
|
|
|
alloc = self.allocation
|
|
|
|
|
2009-07-10 05:58:13 +02:00
|
|
|
self.get_style().paint_box(event.window,
|
|
|
|
gtk.STATE_NORMAL, gtk.SHADOW_IN, event.area, self,
|
2009-07-12 03:42:23 +02:00
|
|
|
'palette-invoker', alloc.x, 0,
|
|
|
|
alloc.width, alloc.height + style._FOCUS_LINE_WIDTH)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-07-12 06:32:51 +02:00
|
|
|
if self.child.state != gtk.STATE_PRELIGHT:
|
2009-07-10 05:58:13 +02:00
|
|
|
self.get_style().paint_box(event.window,
|
|
|
|
gtk.STATE_NORMAL, gtk.SHADOW_NONE, event.area, self, None,
|
2009-07-12 03:42:23 +02:00
|
|
|
alloc.x + style._FOCUS_LINE_WIDTH, style._FOCUS_LINE_WIDTH,
|
2009-07-10 05:58:13 +02:00
|
|
|
alloc.width - style._FOCUS_LINE_WIDTH*2, alloc.height)
|
|
|
|
|
|
|
|
gtk.ToolButton.do_expose_event(self, event)
|
2009-07-30 13:29:52 +02:00
|
|
|
_paint_arrow(self, event, gtk.ARROW_UP)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-07-30 17:25:30 +02:00
|
|
|
class ToolbarBox(gtk.VBox):
|
2009-07-12 06:32:51 +02:00
|
|
|
def __init__(self, padding=style.TOOLBOX_HORIZONTAL_PADDING):
|
2009-07-10 05:58:13 +02:00
|
|
|
gtk.VBox.__init__(self)
|
2009-07-31 09:10:17 +02:00
|
|
|
self.expanded_button = None
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-07-31 12:25:01 +02:00
|
|
|
self._toolbar = gtk.Toolbar()
|
|
|
|
self._toolbar.owner = self
|
|
|
|
self._toolbar.connect('remove', self.__remove_cb)
|
2009-07-12 03:42:23 +02:00
|
|
|
|
2009-07-31 12:25:01 +02:00
|
|
|
top_widget = _embody_page(gtk.EventBox, self._toolbar)
|
2009-07-12 06:32:51 +02:00
|
|
|
self.pack_start(top_widget)
|
|
|
|
|
|
|
|
self.props.padding = padding
|
2009-07-12 03:42:23 +02:00
|
|
|
self.modify_bg(gtk.STATE_NORMAL,
|
|
|
|
style.COLOR_TOOLBAR_GREY.get_gdk_color())
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-07-31 09:10:17 +02:00
|
|
|
def __remove_cb(self, sender, button):
|
|
|
|
if not isinstance(button, ToolbarButton):
|
|
|
|
return
|
|
|
|
button.popdown()
|
|
|
|
if self.expanded_button == button:
|
|
|
|
self.remove(button.page_widget)
|
|
|
|
self.expanded_button = None
|
2009-07-12 03:42:23 +02:00
|
|
|
|
2009-07-31 12:25:01 +02:00
|
|
|
toolbar = property(lambda self: self._toolbar)
|
2009-07-11 16:22:05 +02:00
|
|
|
|
2009-07-12 06:32:51 +02:00
|
|
|
def get_padding(self):
|
2009-07-30 18:25:04 +02:00
|
|
|
return self.toolbar.parent.props.left_padding
|
2009-07-12 06:32:51 +02:00
|
|
|
|
|
|
|
def set_padding(self, pad):
|
2009-07-30 18:25:04 +02:00
|
|
|
self.toolbar.parent.set_padding(0, 0, pad, pad)
|
2009-07-12 06:32:51 +02:00
|
|
|
|
|
|
|
padding = gobject.property(type=object,
|
|
|
|
getter=get_padding, setter=set_padding)
|
|
|
|
|
2009-07-12 03:42:23 +02:00
|
|
|
def modify_bg(self, state, color):
|
|
|
|
if state == gtk.STATE_NORMAL:
|
2009-07-31 09:10:17 +02:00
|
|
|
self.background = color
|
2009-07-30 18:25:04 +02:00
|
|
|
self.toolbar.parent.parent.modify_bg(state, color)
|
|
|
|
self.toolbar.modify_bg(state, color)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-08-01 13:23:06 +02:00
|
|
|
class _ToolbarPalette(PaletteWindow):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
PaletteWindow.__init__(self, **kwargs)
|
|
|
|
self.toolbar_box = None
|
|
|
|
self.set_border_width(0)
|
2009-07-31 22:43:35 +02:00
|
|
|
self._focus = 0
|
|
|
|
|
|
|
|
group = palettegroup.get_group('default')
|
|
|
|
group.connect('popdown', self.__group_popdown_cb)
|
|
|
|
self.set_group_id('toolbar_box')
|
|
|
|
|
|
|
|
def on_invoker_enter(self):
|
|
|
|
PaletteWindow.on_invoker_enter(self)
|
|
|
|
self._handle_focus(+1)
|
|
|
|
|
|
|
|
def on_invoker_leave(self):
|
|
|
|
PaletteWindow.on_invoker_leave(self)
|
|
|
|
self._handle_focus(-1)
|
|
|
|
|
|
|
|
def on_enter(self, event):
|
|
|
|
PaletteWindow.on_enter(self, event)
|
|
|
|
self._handle_focus(+1)
|
|
|
|
|
|
|
|
def on_leave(self, event):
|
|
|
|
PaletteWindow.on_enter(self, event)
|
|
|
|
self._handle_focus(-1)
|
|
|
|
|
|
|
|
def _handle_focus(self, delta):
|
|
|
|
self._focus += delta
|
|
|
|
if self._focus not in (0, 1):
|
|
|
|
logging.error('_Palette._focus=%s not in (0, 1)' % self._focus)
|
|
|
|
|
|
|
|
if self._focus == 0:
|
|
|
|
group = palettegroup.get_group('default')
|
|
|
|
if not group.is_up():
|
|
|
|
self.popdown()
|
2009-08-01 13:23:06 +02:00
|
|
|
|
|
|
|
def do_size_request(self, requisition):
|
|
|
|
gtk.Window.do_size_request(self, requisition)
|
|
|
|
requisition.width = max(requisition.width,
|
|
|
|
gtk.gdk.screen_width())
|
|
|
|
|
2009-08-01 00:04:28 +02:00
|
|
|
def popup(self, immediate=False, state=None):
|
2009-08-01 13:23:06 +02:00
|
|
|
button = self.toolbar_button
|
|
|
|
if button.is_expanded():
|
|
|
|
return
|
|
|
|
box = button.toolbar_box
|
|
|
|
_setup_page(button.page_widget, style.COLOR_BLACK.get_gdk_color(),
|
|
|
|
box.props.padding)
|
2009-08-01 00:04:28 +02:00
|
|
|
PaletteWindow.popup(self, immediate, state)
|
2009-07-31 22:43:35 +02:00
|
|
|
|
|
|
|
def __group_popdown_cb(self, group):
|
|
|
|
if self._focus == 0:
|
|
|
|
self.popdown(immediate=True)
|
2009-08-01 13:23:06 +02:00
|
|
|
|
2009-07-10 05:58:13 +02:00
|
|
|
class _Box(gtk.EventBox):
|
|
|
|
def __init__(self):
|
|
|
|
gtk.EventBox.__init__(self)
|
|
|
|
self.connect('expose-event', self.do_expose_event)
|
|
|
|
self.set_app_paintable(True)
|
|
|
|
|
|
|
|
def do_expose_event(self, widget, event):
|
2009-07-31 12:25:01 +02:00
|
|
|
alloc = self.toolbar_button.allocation
|
2009-07-10 05:58:13 +02:00
|
|
|
self.get_style().paint_box(event.window,
|
|
|
|
gtk.STATE_NORMAL, gtk.SHADOW_IN, event.area, self,
|
|
|
|
'palette-invoker', -style._FOCUS_LINE_WIDTH, 0,
|
|
|
|
self.allocation.width + style._FOCUS_LINE_WIDTH*2,
|
|
|
|
self.allocation.height + style._FOCUS_LINE_WIDTH)
|
|
|
|
self.get_style().paint_box(event.window,
|
|
|
|
gtk.STATE_NORMAL, gtk.SHADOW_NONE, event.area, self, None,
|
2009-07-31 12:25:01 +02:00
|
|
|
alloc.x + style._FOCUS_LINE_WIDTH, 0,
|
|
|
|
alloc.width - style._FOCUS_LINE_WIDTH*2,
|
|
|
|
style._FOCUS_LINE_WIDTH)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-07-31 22:05:40 +02:00
|
|
|
def _setup_page(page_widget, color, hpad):
|
2009-07-13 03:57:27 +02:00
|
|
|
vpad = style._FOCUS_LINE_WIDTH
|
2009-07-31 22:05:40 +02:00
|
|
|
page_widget.child.set_padding(vpad, vpad, hpad, hpad)
|
|
|
|
|
|
|
|
page = page_widget.child.child
|
2009-07-12 03:42:23 +02:00
|
|
|
page.modify_bg(gtk.STATE_NORMAL, color)
|
2009-07-31 22:05:40 +02:00
|
|
|
if isinstance(page, gtk.Container):
|
|
|
|
for i in page.get_children():
|
|
|
|
i.modify_bg(gtk.STATE_NORMAL, color)
|
|
|
|
|
|
|
|
page_widget.modify_bg(gtk.STATE_NORMAL, color)
|
|
|
|
page_widget.modify_bg(gtk.STATE_PRELIGHT, color)
|
2009-07-10 05:58:13 +02:00
|
|
|
|
2009-07-31 05:49:05 +02:00
|
|
|
def _embody_page(box_class, widget):
|
2009-07-12 06:32:51 +02:00
|
|
|
widget.show()
|
|
|
|
alignment = gtk.Alignment(0.0, 0.0, 1.0, 1.0)
|
|
|
|
alignment.add(widget)
|
|
|
|
alignment.show()
|
2009-07-10 05:58:13 +02:00
|
|
|
box = box_class()
|
2009-07-11 15:22:02 +02:00
|
|
|
box.modify_bg(gtk.STATE_ACTIVE, style.COLOR_BUTTON_GREY.get_gdk_color())
|
2009-07-12 06:32:51 +02:00
|
|
|
box.add(alignment)
|
2009-07-10 05:58:13 +02:00
|
|
|
box.show()
|
|
|
|
return box
|
|
|
|
|
|
|
|
def _paint_arrow(widget, event, type):
|
2009-07-30 13:29:52 +02:00
|
|
|
alloc = widget.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-10 05:58:13 +02:00
|
|
|
widget.get_style().paint_arrow(event.window,
|
2009-07-30 13:29:52 +02:00
|
|
|
gtk.STATE_NORMAL, gtk.SHADOW_NONE, event.area, widget,
|
2009-07-10 05:58:13 +02:00
|
|
|
None, type, True,
|
2009-07-30 13:29:52 +02:00
|
|
|
x, y, style.TOOLBAR_ARROW_SIZE, style.TOOLBAR_ARROW_SIZE)
|