sugar-toolkit-gtk3/sugar/graphics/notebook.py

116 lines
3.5 KiB
Python
Raw Normal View History

"""Notebook class
This class create a gtk.Notebook() widget supporting
a close button in every tab when the 'can-close-tabs' gproperty
is enabled (True)
"""
2007-05-06 17:03:43 +02:00
# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com)
#
2007-06-14 22:04:55 +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.
2007-05-06 17:03:43 +02:00
#
2007-06-14 22:04:55 +02:00
# This library is distributed in the hope that it will be useful,
2007-05-06 17:03:43 +02:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2007-06-14 22:04:55 +02:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
2007-05-06 17:03:43 +02:00
#
2007-06-14 22:04:55 +02:00
# 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.
2007-05-06 17:03:43 +02:00
import gtk
import gobject
2007-05-06 17:03:43 +02:00
class Notebook(gtk.Notebook):
__gtype_name__ = 'SugarNotebook'
__gproperties__ = {
'can-close-tabs': (bool, None, None, False,
gobject.PARAM_READWRITE | gobject.PARAM_CONSTRUCT_ONLY)
}
def __init__(self, **kwargs):
# Initialise the Widget
#
# Side effects:
# Set the 'can-close-tabs' property using **kwargs
# Set True the scrollable notebook property
gobject.GObject.__init__(self, **kwargs)
2007-05-06 17:03:43 +02:00
gtk.Notebook.__init__(self)
self.set_scrollable(True)
2007-05-06 17:03:43 +02:00
self.show()
def do_set_property(self, pspec, value):
if pspec.name == 'can-close-tabs':
self._can_close_tabs = value
else:
raise AssertionError
2007-05-06 17:03:43 +02:00
def _add_icon_to_button(self, button):
icon_box = gtk.HBox()
2007-05-06 17:03:43 +02:00
image = gtk.Image()
image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
gtk.Button.set_relief(button, gtk.RELIEF_NONE)
settings = gtk.Widget.get_settings(button)
(w,h) = gtk.icon_size_lookup_for_settings(settings, gtk.ICON_SIZE_MENU)
gtk.Widget.set_size_request(button, w + 4, h + 4)
2007-05-06 17:03:43 +02:00
image.show()
icon_box.pack_start(image, True, False, 0)
button.add(icon_box)
icon_box.show()
2007-05-06 17:03:43 +02:00
def _create_custom_tab(self, text, child):
event_box = gtk.EventBox()
tab_box = gtk.HBox(False, 2)
tab_label = gtk.Label(text)
2007-05-06 17:03:43 +02:00
tab_button = gtk.Button()
tab_button.connect('clicked', self._close_page, child)
2007-05-06 17:03:43 +02:00
# Add a picture on a button
self._add_icon_to_button(tab_button)
icon_box = gtk.HBox(False, 0)
2007-05-06 17:03:43 +02:00
event_box.show()
tab_button.show()
tab_label.show()
2007-05-06 17:03:43 +02:00
tab_box.pack_start(tab_label, True)
tab_box.pack_start(tab_button, True)
2007-05-06 17:03:43 +02:00
tab_box.show_all()
event_box.add(tab_box)
2007-05-06 17:03:43 +02:00
return event_box
2007-05-06 17:03:43 +02:00
def add_page(self, text_label, widget):
# Add a new page to the notebook
if self._can_close_tabs:
eventbox = self._create_custom_tab(text_label, widget)
self.append_page(widget, eventbox)
else:
self.append_page(widget, gtk.Label(text_label))
pages = self.get_n_pages()
2007-05-06 17:03:43 +02:00
# Set the new page
2007-05-06 17:03:43 +02:00
self.set_current_page(pages - 1)
self.show_all()
2007-05-06 17:03:43 +02:00
return True
def _close_page(self, button, child):
# Remove a page from the notebook
2007-05-06 17:03:43 +02:00
page = self.page_num(child)
if page != -1:
self.remove_page(page)