3
This class create a gtk.Notebook() widget supporting
4
a close button in every tab when the 'can-close-tabs' gproperty
8
# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com)
10
# This library is free software; you can redistribute it and/or
11
# modify it under the terms of the GNU Lesser General Public
12
# License as published by the Free Software Foundation; either
13
# version 2 of the License, or (at your option) any later version.
15
# This library is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
# Lesser General Public License for more details.
20
# You should have received a copy of the GNU Lesser General Public
21
# License along with this library; if not, write to the
22
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23
# Boston, MA 02111-1307, USA.
28
class Notebook(gtk.Notebook):
29
__gtype_name__ = 'SugarNotebook'
32
'can-close-tabs': (bool, None, None, False,
33
gobject.PARAM_READWRITE | gobject.PARAM_CONSTRUCT_ONLY)
36
def __init__(self, **kwargs):
37
# Initialise the Widget
40
# Set the 'can-close-tabs' property using **kwargs
41
# Set True the scrollable notebook property
43
gobject.GObject.__init__(self, **kwargs)
44
gtk.Notebook.__init__(self)
46
self.set_scrollable(True)
49
def do_set_property(self, pspec, value):
50
if pspec.name == 'can-close-tabs':
51
self._can_close_tabs = value
55
def _add_icon_to_button(self, button):
58
image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
59
gtk.Button.set_relief(button, gtk.RELIEF_NONE)
61
settings = gtk.Widget.get_settings(button)
62
(w,h) = gtk.icon_size_lookup_for_settings(settings, gtk.ICON_SIZE_MENU)
63
gtk.Widget.set_size_request(button, w + 4, h + 4)
65
icon_box.pack_start(image, True, False, 0)
69
def _create_custom_tab(self, text, child):
70
event_box = gtk.EventBox()
72
tab_box = gtk.HBox(False, 2)
73
tab_label = gtk.Label(text)
75
tab_button = gtk.Button()
76
tab_button.connect('clicked', self._close_page, child)
78
# Add a picture on a button
79
self._add_icon_to_button(tab_button)
80
icon_box = gtk.HBox(False, 0)
86
tab_box.pack_start(tab_label, True)
87
tab_box.pack_start(tab_button, True)
90
event_box.add(tab_box)
94
def add_page(self, text_label, widget):
95
# Add a new page to the notebook
96
if self._can_close_tabs:
97
eventbox = self._create_custom_tab(text_label, widget)
98
self.append_page(widget, eventbox)
100
self.append_page(widget, gtk.Label(text_label))
102
pages = self.get_n_pages()
105
self.set_current_page(pages - 1)
110
def _close_page(self, button, child):
111
# Remove a page from the notebook
112
page = self.page_num(child)
115
self.remove_page(page)