1### Copyright (C) 2002-2009 Stephen Kennedy <stevek@gnome.org>
2### Copyright (C) 2008-2009 Kai Willadsen <kai.willadsen@gmail.com>
3
4### This program is free software; you can redistribute it and/or modify
5### it under the terms of the GNU General Public License as published by
6### the Free Software Foundation; either version 2 of the License, or
7### (at your option) any later version.
8
9### This program is distributed in the hope that it will be useful,
10### but WITHOUT ANY WARRANTY; without even the implied warranty of
11### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12### GNU General Public License for more details.
13
14### You should have received a copy of the GNU General Public License
15### along with this program; if not, write to the Free Software
16### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
17### USA.
18
19
20from gettext import gettext as _
21
22import gtk
23import pango
24
25gtk.rc_parse_string(
26    """
27    style "meld-tab-close-button-style" {
28        GtkWidget::focus-padding = 0
29        GtkWidget::focus-line-width = 0
30        xthickness = 0
31        ythickness = 0
32    }
33    widget "*.meld-tab-close-button" style "meld-tab-close-button-style"
34    """)
35
36class NotebookLabel(gtk.HBox):
37    __gtype_name__ = "NotebookLabel"
38
39    tab_width_in_chars = 30
40
41    def __init__(self, iconname, text, onclose):
42        gtk.HBox.__init__(self, False, 4)
43
44        label = gtk.Label(text)
45        # FIXME: ideally, we would use custom ellipsization that ellipsized the
46        # two paths separately, but that requires significant changes to label
47        # generation in many different parts of the code
48        label.set_ellipsize(pango.ELLIPSIZE_MIDDLE)
49        label.set_single_line_mode(True)
50        label.set_alignment(0.0, 0.5)
51        label.set_padding(0, 0)
52
53        context = self.get_pango_context()
54        metrics = context.get_metrics(self.style.font_desc, context.get_language())
55        char_width = metrics.get_approximate_digit_width()
56        (w, h) = gtk.icon_size_lookup_for_settings (self.get_settings(), gtk.ICON_SIZE_MENU)
57        self.set_size_request(self.tab_width_in_chars * pango.PIXELS(char_width) + 2 * w, -1)
58
59        button = gtk.Button()
60        button.set_relief(gtk.RELIEF_NONE)
61        button.set_focus_on_click(False)
62        image = gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
63        image.set_tooltip_text(_("Close tab"))
64        button.add(image)
65        button.set_name("meld-tab-close-button")
66        button.set_size_request(w + 2, h + 2)
67        button.connect("clicked", onclose)
68
69        icon = gtk.image_new_from_icon_name(iconname, gtk.ICON_SIZE_MENU)
70
71        label_box = gtk.EventBox()
72        label_box.add_events(gtk.gdk.BUTTON_PRESS_MASK)
73        label_box.props.visible_window = False
74        label_box.connect("button-press-event", self.on_label_clicked)
75        label_box.add(label)
76
77        self.pack_start(icon, expand=False)
78        self.pack_start(label_box)
79        self.pack_start(button, expand=False)
80        self.set_tooltip_text(text)
81        self.show_all()
82
83        self.__label = label
84        self.__onclose = onclose
85
86    def on_label_clicked(self, box, event):
87        if event.type == gtk.gdk.BUTTON_PRESS and event.button == 2:
88            self.__onclose(None)
89
90    def get_label_text(self):
91        return self.__label.get_text()
92
93    def set_label_text(self, text):
94        self.__label.set_text(text)
95        self.set_tooltip_text(text)
96
97