Notebook widget class: new 'can-close-tabs' property

This commit is contained in:
Eduardo Silva 2007-05-08 09:32:32 -04:00
parent 110fa5f354
commit 9180388a76
2 changed files with 52 additions and 39 deletions

View File

@ -17,67 +17,82 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import gtk import gtk
import gobject
class Notebook(gtk.Notebook): class Notebook(gtk.Notebook):
__gtype_name__ = 'SugarNotebook'
page_number = 0
__gproperties__ = {
'can-close-tabs': (bool, None, None, True,
gobject.PARAM_READWRITE)
}
def __init__(self): def __init__(self):
gtk.Notebook.__init__(self) gtk.Notebook.__init__(self)
self._can_close_tabs = False
self.set_scrollable(True) self.set_scrollable(True)
t_width = gtk.gdk.screen_width()
t_height = gtk.gdk.screen_height() * 83 / 100
self.set_size_request(t_width, t_height)
self.show() self.show()
def do_set_property(self, pspec, value):
if pspec.name == 'can-close-tabs':
self._can_close_tabs = value
else:
raise AssertionError
def _add_icon_to_button(self, button): def _add_icon_to_button(self, button):
iconBox = gtk.HBox(False, 0) icon_box = gtk.HBox()
image = gtk.Image() image = gtk.Image()
image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
gtk.Button.set_relief(button, gtk.RELIEF_NONE) gtk.Button.set_relief(button, gtk.RELIEF_NONE)
settings = gtk.Widget.get_settings (button) settings = gtk.Widget.get_settings(button)
(w,h) = gtk.icon_size_lookup_for_settings (settings, gtk.ICON_SIZE_MENU) (w,h) = gtk.icon_size_lookup_for_settings(settings, gtk.ICON_SIZE_MENU)
gtk.Widget.set_size_request (button, w + 4, h + 4) gtk.Widget.set_size_request(button, w + 4, h + 4)
image.show() image.show()
iconBox.pack_start(image, True, False, 0) icon_box.pack_start(image, True, False, 0)
button.add(iconBox) button.add(icon_box)
iconBox.show() icon_box.show()
def _create_custom_tab(self, text, child): def _create_custom_tab(self, text, child):
eventBox = gtk.EventBox() event_box = gtk.EventBox()
tabBox = gtk.HBox(False, 2)
tabLabel = gtk.Label(text)
tabButton = gtk.Button() tab_box = gtk.HBox(False, 2)
tabButton.connect('clicked', self.close_page, child) tab_label = gtk.Label(text)
tab_button = gtk.Button()
tab_button.connect('clicked', self.close_page, child)
# Add a picture on a button # Add a picture on a button
self._add_icon_to_button(tabButton) self._add_icon_to_button(tab_button)
iconBox = gtk.HBox(False, 0) icon_box = gtk.HBox(False, 0)
eventBox.show() event_box.show()
tabButton.show() tab_button.show()
tabLabel.show() tab_label.show()
tabBox.pack_start(tabLabel, False) tab_box.pack_start(tab_label, True)
tabBox.pack_start(tabButton, False) tab_box.pack_start(tab_button, True)
tabBox.show_all() tab_box.show_all()
eventBox.add(tabBox) event_box.add(tab_box)
return eventBox return event_box
# Add a new page to the notebook # Add a new page to the notebook
def add_page(self, text_label, widget): def add_page(self, text_label, widget):
eventBox = self._create_custom_tab(text_label, widget) if self._can_close_tabs:
self.append_page(widget, eventBox) 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()
# Set the new page # Set the new page
pages = self.get_n_pages()
self.set_current_page(pages - 1) self.set_current_page(pages - 1)
self.show_all()
return True return True
# Remove a page from the notebook # Remove a page from the notebook
@ -86,8 +101,3 @@ class Notebook(gtk.Notebook):
if page != -1: if page != -1:
self.remove_page(page) self.remove_page(page)
# Need to refresh the widget --
# This forces the widget to redraw itself.
self.queue_draw_area(0, 0, -1, -1)

View File

@ -24,8 +24,12 @@ from sugar.graphics.notebook import Notebook
window = gtk.Window() window = gtk.Window()
window.connect("destroy", lambda w: gtk.main_quit()) window.connect("destroy", lambda w: gtk.main_quit())
window.set_size_request(800, 600)
window.show_all()
nb = Notebook() nb = Notebook()
nb.set_property('can-close-tabs', True)
window.add(nb) window.add(nb)
button1 = gtk.Button('Example 1') button1 = gtk.Button('Example 1')
@ -36,5 +40,4 @@ nb.add_page('Testing label 1', button1)
nb.add_page('Testing label 2', button2) nb.add_page('Testing label 2', button2)
nb.add_page('Testing label 3', button3) nb.add_page('Testing label 3', button3)
window.show_all()
gtk.main() gtk.main()