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
|
|
|
|
2016-01-01 00:14:26 +01:00
|
|
|
'''
|
|
|
|
Notebook class
|
2008-10-28 14:19:01 +01:00
|
|
|
|
2016-01-01 00:14:26 +01:00
|
|
|
This class creates a :class:`Gtk.Notebook` widget supporting
|
|
|
|
a close button in every tab when the `can-close-tabs` gproperty
|
|
|
|
is enabled (True).
|
2008-10-28 14:19:01 +01:00
|
|
|
|
2016-01-01 00:14:26 +01:00
|
|
|
.. literalinclude:: ../examples/notebook.py
|
|
|
|
'''
|
2008-10-28 14:19:01 +01:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import Gtk
|
|
|
|
from gi.repository import GObject
|
2007-05-06 17:03:43 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class Notebook(Gtk.Notebook):
|
2016-01-01 00:14:26 +01:00
|
|
|
'''
|
|
|
|
Notebook class that creates a :class:`Gtk.Notebook`. It is possible to set
|
|
|
|
the `can-close-tabs` property from the constructor through Notebook(can_close_tabs=True)
|
|
|
|
'''
|
2007-05-08 15:32:32 +02:00
|
|
|
__gtype_name__ = 'SugarNotebook'
|
|
|
|
|
|
|
|
__gproperties__ = {
|
2007-05-09 20:38:38 +02:00
|
|
|
'can-close-tabs': (bool, None, None, False,
|
2013-05-18 04:27:04 +02:00
|
|
|
GObject.PARAM_READWRITE |
|
|
|
|
GObject.PARAM_CONSTRUCT_ONLY),
|
2007-05-08 15:32:32 +02:00
|
|
|
}
|
|
|
|
|
2007-05-09 20:38:38 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
# Initialise the Widget
|
2009-08-25 19:55:48 +02:00
|
|
|
|
|
|
|
# Side effects:
|
2007-05-09 20:38:38 +02:00
|
|
|
# Set the 'can-close-tabs' property using **kwargs
|
|
|
|
# Set True the scrollable notebook property
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2008-04-19 01:10:48 +02:00
|
|
|
self._can_close_tabs = None
|
2011-11-04 10:43:23 +01:00
|
|
|
GObject.GObject.__init__(self, **kwargs)
|
2007-05-06 17:03:43 +02:00
|
|
|
|
2007-05-08 15:32:32 +02:00
|
|
|
self.set_scrollable(True)
|
2007-05-06 17:03:43 +02:00
|
|
|
self.show()
|
2007-05-09 20:38:38 +02:00
|
|
|
|
2007-05-08 15:32:32 +02:00
|
|
|
def do_set_property(self, pspec, value):
|
2016-01-01 00:14:26 +01:00
|
|
|
'''
|
|
|
|
Implementation method. Use notebook.props to set properties.
|
|
|
|
'''
|
2007-05-08 15:32:32 +02:00
|
|
|
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):
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_box = Gtk.HBox()
|
|
|
|
image = Gtk.Image()
|
|
|
|
image.set_from_stock(Gtk.STOCK_CLOSE, Gtk.IconSize.MENU)
|
|
|
|
Gtk.Button.set_relief(button, Gtk.ReliefStyle.NONE)
|
|
|
|
|
|
|
|
settings = Gtk.Widget.get_settings(button)
|
2011-11-04 09:30:01 +01:00
|
|
|
valid_, w, h = Gtk.icon_size_lookup_for_settings(settings,
|
|
|
|
Gtk.IconSize.MENU)
|
2011-11-15 19:29:07 +01:00
|
|
|
Gtk.Widget.set_size_request(button, w + 4, h + 4)
|
2007-05-06 17:03:43 +02:00
|
|
|
image.show()
|
2007-05-08 15:32:32 +02:00
|
|
|
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):
|
2011-11-15 19:29:07 +01:00
|
|
|
event_box = Gtk.EventBox()
|
2007-05-08 15:32:32 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
tab_box = Gtk.HBox(False, 2)
|
|
|
|
tab_label = Gtk.Label(label=text)
|
2007-05-06 17:03:43 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
tab_button = Gtk.Button()
|
2007-05-09 04:38:56 +02:00
|
|
|
tab_button.connect('clicked', self._close_page, child)
|
2007-05-06 17:03:43 +02:00
|
|
|
|
|
|
|
# Add a picture on a button
|
2007-05-08 15:32:32 +02:00
|
|
|
self._add_icon_to_button(tab_button)
|
2007-05-06 17:03:43 +02:00
|
|
|
|
2007-05-08 15:32:32 +02:00
|
|
|
event_box.show()
|
|
|
|
tab_button.show()
|
|
|
|
tab_label.show()
|
2007-05-06 17:03:43 +02:00
|
|
|
|
2011-10-30 08:01:32 +01:00
|
|
|
tab_box.pack_start(tab_label, True, False, 0)
|
|
|
|
tab_box.pack_start(tab_button, True, False, 0)
|
2007-05-06 17:03:43 +02:00
|
|
|
|
2007-05-08 15:32:32 +02:00
|
|
|
tab_box.show_all()
|
|
|
|
event_box.add(tab_box)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-05-08 15:32:32 +02:00
|
|
|
return event_box
|
2007-05-06 17:03:43 +02:00
|
|
|
|
|
|
|
def add_page(self, text_label, widget):
|
2016-01-01 00:14:26 +01:00
|
|
|
'''
|
|
|
|
Adds a page to the notebook and sets the newly added page as current.
|
|
|
|
Returns True if the page is successfully added to the notebook.
|
|
|
|
If `can-close-tabs` is true, then a GtkEventBox is also created to close the tab.
|
2008-09-21 04:22:56 +02:00
|
|
|
|
2016-01-01 00:14:26 +01:00
|
|
|
Args:
|
|
|
|
text_label (string): label of page to be added
|
2008-09-21 04:22:56 +02:00
|
|
|
|
2016-01-01 00:14:26 +01:00
|
|
|
widget (Gtk.Widget): widget to be used as contents of current page
|
|
|
|
'''
|
2007-05-09 20:38:38 +02:00
|
|
|
# Add a new page to the notebook
|
2007-05-08 15:32:32 +02:00
|
|
|
if self._can_close_tabs:
|
|
|
|
eventbox = self._create_custom_tab(text_label, widget)
|
2009-08-25 19:55:48 +02:00
|
|
|
self.append_page(widget, eventbox)
|
2007-05-08 15:32:32 +02:00
|
|
|
else:
|
2011-11-15 19:29:07 +01:00
|
|
|
self.append_page(widget, Gtk.Label(label=text_label))
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-05-08 15:32:32 +02:00
|
|
|
pages = self.get_n_pages()
|
2007-05-06 17:03:43 +02:00
|
|
|
|
2007-05-09 20:38:38 +02:00
|
|
|
# Set the new page
|
2007-05-06 17:03:43 +02:00
|
|
|
self.set_current_page(pages - 1)
|
2007-05-08 15:32:32 +02:00
|
|
|
self.show_all()
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-05-06 17:03:43 +02:00
|
|
|
return True
|
|
|
|
|
2007-05-09 04:38:56 +02:00
|
|
|
def _close_page(self, button, child):
|
2007-05-09 20:38:38 +02:00
|
|
|
# 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)
|