<commit_before># 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

from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.palette import _PopupAnimation, _PopdownAnimation
from sugar.graphics.palette import MouseSpeedDetector, Invoker
from sugar.graphics import animator

class ToolbarButton(ToolButton):
    def __init__(self, **kwargs):
        self._page = None

        ToolButton.__init__(self, **kwargs)

        if self.palette is None:
            self.palette = _Palette(self)

        self.connect('clicked',
                lambda widget: self.set_expanded(not self.expanded))

    def get_toolbar(self):
        if not hasattr(self.parent, 'owner'):
            return None
        return self.parent.owner

    toolbar = property(get_toolbar)

    def get_page(self):
        return self._page.child.child

    def set_page(self, page):
        self._page = _align(_Box, page)
        self._page._toolitem = self
        page.show()

    page = gobject.property(type=object, getter=get_page, setter=set_page)

    def get_expanded(self):
        return bool(self.toolbar) and bool(self._page) and \
                self.toolbar._expanded_page() == self._page

    def set_expanded(self, value):
        if not self.toolbar or not self._page or self.get_expanded() == value:
            return

        if isinstance(self.palette, _Palette) and self.palette.is_up():
            self.palette.popdown(immediate=True)

        if not value:
            self.toolbar._shrink_page(self._page)
            return

        expanded = self.toolbar._expanded_page()
        if expanded and expanded._toolitem.window:
            expanded._toolitem.window.invalidate_rect(None, True)

        if self._page.parent:
            self.palette.remove(self._page)

        self.modify_bg(gtk.STATE_NORMAL, self.toolbar._bg)

        self.toolbar._expand_page(self._page)

    expanded = property(get_expanded, set_expanded)

    def do_expose_event(self, event):
        if not self.expanded or self.palette and self.palette.is_up():
            ToolButton.do_expose_event(self, event)
            if self.palette and self.palette.is_up():
                _paint_arrow(self, event, gtk.ARROW_DOWN)
            else:
                _paint_arrow(self, event, gtk.ARROW_UP)
            return

        alloc = self.allocation

        self.get_style().paint_box(event.window,
                gtk.STATE_NORMAL, gtk.SHADOW_IN, event.area, self,
                'palette-invoker', alloc.x, 0,
                alloc.width, alloc.height + style._FOCUS_LINE_WIDTH)

        if self.child.state != gtk.STATE_PRELIGHT:
            self.get_style().paint_box(event.window,
                    gtk.STATE_NORMAL, gtk.SHADOW_NONE, event.area, self, None,
                    alloc.x + style._FOCUS_LINE_WIDTH, style._FOCUS_LINE_WIDTH,
                    alloc.width - style._FOCUS_LINE_WIDTH*2, alloc.height)

        gtk.ToolButton.do_expose_event(self, event)
        _paint_arrow(self, event, gtk.ARROW_DOWN)

class Toolbar(gtk.VBox):
    __gsignals__ = {
        'current-toolbar-changed': (SIGNAL_RUN_FIRST, TYPE_NONE, ([int]))
        }

    def __init__(self, padding=style.TOOLBOX_HORIZONTAL_PADDING):
        gtk.VBox.__init__(self)

        self._top = gtk.Toolbar()
        self._top.owner = self

        top_widget = _align(gtk.EventBox, self._top)
        self.pack_start(top_widget)

        self.props.padding = padding
        self.modify_bg(gtk.STATE_NORMAL,
                style.COLOR_TOOLBAR_GREY.get_gdk_color())

        self._notebook = gtk.Notebook()
        self._notebook.set_show_border(False)
        self._notebook.set_show_tabs(False)
        self._notebook.show()

        self._notebook.connect('notify::page', lambda notebook, pspec:
                self.emit('current-toolbar-changed', notebook.props.page))

        self._top.connect('remove', self._remove_cb)

    top = property(lambda self: self._top)

    def get_padding(self):
        return self._top.parent.props.left_padding

    def set_padding(self, pad):
        self._top.parent.set_padding(0, 0, pad, pad)

    padding = gobject.property(type=object,
            getter=get_padding, setter=set_padding)

    def modify_bg(self, state, color):
        if state == gtk.STATE_NORMAL:
            self._bg = color
        self._top.parent.parent.modify_bg(state, color)
        self._top.modify_bg(state, color)

    def _remove_cb(self, sender, widget):
        if not isinstance(widget, ToolbarButton):
            return
        page_no = self._notebook.page_num(widget._page)
        if page_no != -1:
            self._notebook.remove_page(page_no)

    def _expanded_page(self):
        if self._notebook.parent is None:<commit_msg>Close palette while removing ToolbarButton<commit_after># 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

from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.palette import _PopupAnimation, _PopdownAnimation
from sugar.graphics.palette import MouseSpeedDetector, Invoker
from sugar.graphics import animator

class ToolbarButton(ToolButton):
    def __init__(self, **kwargs):
        self._page = None

        ToolButton.__init__(self, **kwargs)

        if self.palette is None:
            self.palette = _Palette(self)

        self.connect('clicked',
                lambda widget: self.set_expanded(not self.expanded))

    def get_toolbar(self):
        if not hasattr(self.parent, 'owner'):
            return None
        return self.parent.owner

    toolbar = property(get_toolbar)

    def get_page(self):
        return self._page.child.child

    def set_page(self, page):
        self._page = _align(_Box, page)
        self._page._toolitem = self
        page.show()

    page = gobject.property(type=object, getter=get_page, setter=set_page)

    def get_expanded(self):
        return bool(self.toolbar) and bool(self._page) and \
                self.toolbar._expanded_page() == self._page

    def set_expanded(self, value):
        if not self.toolbar or not self._page or self.get_expanded() == value:
            return

        if isinstance(self.palette, _Palette) and self.palette.is_up():
            self.palette.popdown(immediate=True)

        if not value:
            self.toolbar._shrink_page(self._page)
            return

        expanded = self.toolbar._expanded_page()
        if expanded and expanded._toolitem.window:
            expanded._toolitem.window.invalidate_rect(None, True)

        if self._page.parent:
            self.palette.remove(self._page)

        self.modify_bg(gtk.STATE_NORMAL, self.toolbar._bg)

        self.toolbar._expand_page(self._page)

    expanded = property(get_expanded, set_expanded)

    def do_expose_event(self, event):
        if not self.expanded or self.palette and self.palette.is_up():
            ToolButton.do_expose_event(self, event)
            if self.palette and self.palette.is_up():
                _paint_arrow(self, event, gtk.ARROW_DOWN)
            else:
                _paint_arrow(self, event, gtk.ARROW_UP)
            return

        alloc = self.allocation

        self.get_style().paint_box(event.window,
                gtk.STATE_NORMAL, gtk.SHADOW_IN, event.area, self,
                'palette-invoker', alloc.x, 0,
                alloc.width, alloc.height + style._FOCUS_LINE_WIDTH)

        if self.child.state != gtk.STATE_PRELIGHT:
            self.get_style().paint_box(event.window,
                    gtk.STATE_NORMAL, gtk.SHADOW_NONE, event.area, self, None,
                    alloc.x + style._FOCUS_LINE_WIDTH, style._FOCUS_LINE_WIDTH,
                    alloc.width - style._FOCUS_LINE_WIDTH*2, alloc.height)

        gtk.ToolButton.do_expose_event(self, event)
        _paint_arrow(self, event, gtk.ARROW_DOWN)

class Toolbar(gtk.VBox):
    __gsignals__ = {
        'current-toolbar-changed': (SIGNAL_RUN_FIRST, TYPE_NONE, ([int]))
        }

    def __init__(self, padding=style.TOOLBOX_HORIZONTAL_PADDING):
        gtk.VBox.__init__(self)

        self._top = gtk.Toolbar()
        self._top.owner = self

        top_widget = _align(gtk.EventBox, self._top)
        self.pack_start(top_widget)

        self.props.padding = padding
        self.modify_bg(gtk.STATE_NORMAL,
                style.COLOR_TOOLBAR_GREY.get_gdk_color())

        self._notebook = gtk.Notebook()
        self._notebook.set_show_border(False)
        self._notebook.set_show_tabs(False)
        self._notebook.show()

        self._notebook.connect('notify::page', lambda notebook, pspec:
                self.emit('current-toolbar-changed', notebook.props.page))

        self._top.connect('remove', self._remove_cb)

    top = property(lambda self: self._top)

    def get_padding(self):
        return self._top.parent.props.left_padding

    def set_padding(self, pad):
        self._top.parent.set_padding(0, 0, pad, pad)

    padding = gobject.property(type=object,
            getter=get_padding, setter=set_padding)

    def modify_bg(self, state, color):
        if state == gtk.STATE_NORMAL:
            self._bg = color
        self._top.parent.parent.modify_bg(state, color)
        self._top.modify_bg(state, color)

    def _remove_cb(self, sender, widget):
        if not isinstance(widget, ToolbarButton):
            return
        page_no = self._notebook.page_num(widget._page)
        if page_no != -1:
            self._notebook.remove_page(page_no)
        if widget.palette:
            widget.palette.popdown(immediate=True)

    def _expanded_page(self):
        if self._notebook.parent is None:
{'fraction_non_alphanumeric': 0.09216300940438872, 'fraction_numerical': 0.0035826242722794446, 'mean_word_length': 2.617103984450923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4913115', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3309, 'n_words': 796}