1# Copyright (C) 2002-2009 Stephen Kennedy <stevek@gnome.org>
2# Copyright (C) 2008-2009, 2013 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 (at
7# your option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# 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, see <http://www.gnu.org/licenses/>.
16
17from gi.repository import Gdk
18from gi.repository import Gio
19from gi.repository import Gtk
20from gi.repository import Pango
21
22from meld.conf import _
23
24
25class NotebookLabel(Gtk.HBox):
26    __gtype_name__ = "NotebookLabel"
27
28    tab_width_in_chars = 30
29
30    def __init__(self, iconname, text, onclose):
31        super().__init__(homogeneous=False, spacing=4)
32
33        label = Gtk.Label(label=text)
34        # FIXME: ideally, we would use custom ellipsization that ellipsized the
35        # two paths separately, but that requires significant changes to label
36        # generation in many different parts of the code
37        label.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
38        label.set_single_line_mode(True)
39        label.set_alignment(0.0, 0.5)
40        label.set_padding(0, 0)
41
42        style_context = self.get_style_context()
43        style_context.save()
44        style_context.set_state(Gtk.StateFlags.NORMAL)
45        font_desc = style_context.get_font(style_context.get_state())
46        style_context.restore()
47
48        context = self.get_pango_context()
49        metrics = context.get_metrics(font_desc, context.get_language())
50        char_width = metrics.get_approximate_char_width() / Pango.SCALE
51        valid, w, h = Gtk.icon_size_lookup_for_settings(
52            self.get_settings(), Gtk.IconSize.MENU)
53        # FIXME: PIXELS replacement
54        self.set_size_request(
55            self.tab_width_in_chars * char_width + 2 * w, -1)
56
57        button = Gtk.Button()
58        button.set_relief(Gtk.ReliefStyle.NONE)
59        button.set_focus_on_click(False)
60        icon = Gio.ThemedIcon.new_with_default_fallbacks(
61            'window-close-symbolic')
62        image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.MENU)
63        image.set_tooltip_text(_("Close tab"))
64        button.add(image)
65        button.connect("clicked", onclose)
66
67        icon = Gtk.Image.new_from_icon_name(iconname, Gtk.IconSize.MENU)
68
69        label_box = Gtk.EventBox()
70        label_box.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
71        label_box.props.visible_window = False
72        label_box.connect("button-press-event", self.on_label_clicked)
73        label_box.add(label)
74
75        self.pack_start(icon, False, True, 0)
76        self.pack_start(label_box, True, True, 0)
77        self.pack_start(button, False, True, 0)
78        self.set_tooltip_text(text)
79        self.show_all()
80
81        self.__label = label
82        self.__onclose = onclose
83
84    def on_label_clicked(self, box, event):
85        if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 2:
86            self.__onclose(None)
87
88    def get_label_text(self):
89        return self.__label.get_text()
90
91    def set_label_text(self, text):
92        self.__label.set_text(text)
93        self.set_tooltip_text(text)
94