~ubuntu-branches/ubuntu/karmic/sugar-toolkit/karmic

« back to all changes in this revision

Viewing changes to sugar/graphics/notebook.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-08-07 19:43:09 UTC
  • mfrom: (0.1.2 lenny) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080807194309-03302c4lj0j0ipze
Tags: 0.82.0-1
* New upstream release.
* Unfuzz patch 2991.
* Add DEB_MAINTAINER_MODE in debian/rules (thanks to Romain Beauxis).
* Update copyright-hints.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Notebook class
2
 
 
3
 
This class create a gtk.Notebook() widget supporting 
4
 
a close button in every tab when the 'can-close-tabs' gproperty
5
 
is enabled (True)
6
 
"""
7
 
 
8
 
# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com)
9
 
#
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.
14
 
#
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.
19
 
#
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.
24
 
 
25
 
import gtk
26
 
import gobject
27
 
 
28
 
class Notebook(gtk.Notebook):
29
 
    __gtype_name__ = 'SugarNotebook'
30
 
 
31
 
    __gproperties__ = {
32
 
        'can-close-tabs': (bool, None, None, False,
33
 
                           gobject.PARAM_READWRITE | gobject.PARAM_CONSTRUCT_ONLY)
34
 
    }
35
 
 
36
 
    def __init__(self, **kwargs):
37
 
        # Initialise the Widget
38
 
        #
39
 
        #    Side effects: 
40
 
        #        Set the 'can-close-tabs' property using **kwargs
41
 
        #        Set True the scrollable notebook property
42
 
        
43
 
        gobject.GObject.__init__(self, **kwargs)
44
 
        gtk.Notebook.__init__(self)
45
 
 
46
 
        self.set_scrollable(True)
47
 
        self.show()
48
 
 
49
 
    def do_set_property(self, pspec, value):
50
 
        if pspec.name == 'can-close-tabs':
51
 
            self._can_close_tabs = value
52
 
        else:
53
 
            raise AssertionError
54
 
 
55
 
    def _add_icon_to_button(self, button):
56
 
        icon_box = gtk.HBox()
57
 
        image = gtk.Image()
58
 
        image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
59
 
        gtk.Button.set_relief(button, gtk.RELIEF_NONE)
60
 
 
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)
64
 
        image.show()
65
 
        icon_box.pack_start(image, True, False, 0)
66
 
        button.add(icon_box)
67
 
        icon_box.show()
68
 
 
69
 
    def _create_custom_tab(self, text, child):
70
 
        event_box = gtk.EventBox()
71
 
 
72
 
        tab_box = gtk.HBox(False, 2)
73
 
        tab_label = gtk.Label(text)
74
 
 
75
 
        tab_button = gtk.Button()
76
 
        tab_button.connect('clicked', self._close_page, child)
77
 
 
78
 
        # Add a picture on a button
79
 
        self._add_icon_to_button(tab_button)
80
 
        icon_box = gtk.HBox(False, 0)
81
 
 
82
 
        event_box.show()
83
 
        tab_button.show()
84
 
        tab_label.show()
85
 
 
86
 
        tab_box.pack_start(tab_label, True)
87
 
        tab_box.pack_start(tab_button, True)
88
 
 
89
 
        tab_box.show_all()
90
 
        event_box.add(tab_box)
91
 
        
92
 
        return event_box
93
 
 
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)          
99
 
        else:
100
 
            self.append_page(widget, gtk.Label(text_label))
101
 
            
102
 
        pages = self.get_n_pages()
103
 
 
104
 
        # Set the new page
105
 
        self.set_current_page(pages - 1)
106
 
        self.show_all()
107
 
        
108
 
        return True
109
 
 
110
 
    def _close_page(self, button, child):
111
 
        # Remove a page from the notebook
112
 
        page = self.page_num(child)
113
 
 
114
 
        if page != -1:
115
 
            self.remove_page(page)