aboutsummaryrefslogtreecommitdiff
path: root/src/dash-button.vala
blob: 7668e973377c312cf8b8297afe5a7f5c94dff0f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
 *
 * Copyright (C) 2012 Canonical Ltd
 * Copyright (C) 2014,2015 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Terry <michael.terry@canonical.com>
 *          Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 */

public class DashButton : FlatButton, Fadable
{
    protected FadeTracker fade_tracker { get; protected set; }
    private Gtk.Label text_label;

    public static string font = AGSettings.get_string (AGSettings.KEY_FONT_NAME);
    public static string font_family = font.split_set(" ")[0];
    public static int font_size = int.parse(font.split_set(" ")[1]);

    private string _text = "";
    public string text
    {
        get { return _text; }
        set
        {
            _text = value;
            text_label.set_markup ("<span font=\"%s %d\">%s</span>".printf (font_family, font_size+2, value));
        }
    }
    public DashButton (string text)
    {
        fade_tracker = new FadeTracker (this);

        var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);

        if (font_size < 6)
            font_size = 6;

        /* Add text */
        text_label = new Gtk.Label ("");
        text_label.use_markup = true;
        text_label.hexpand = true;
        text_label.halign = Gtk.Align.START;
        hbox.add (text_label);
        this.text = text;

        /* Add chevron */
        var path = Path.build_filename (Config.PKGDATADIR, "arrow_right.png", null);
        try
        {
            var pixbuf = new Gdk.Pixbuf.from_file (path);
            var image = new CachedImage (pixbuf);
            image.valign = Gtk.Align.CENTER;
            hbox.add (image);
        }
        catch (Error e)
        {
            debug ("Error loading image %s: %s", path, e.message);
        }

        hbox.show_all ();
        add (hbox);

        try
        {
            var style = new Gtk.CssProvider ();
            style.load_from_data ("* {padding: 6px 8px 6px 8px;
                                      outline-width: 0px;
                                     }", -1);
            this.get_style_context ().add_provider (style, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
        }
        catch (Error e)
        {
            debug ("Internal error loading session chooser style: %s", e.message);
        }
    }

    public override bool draw (Cairo.Context c)
    {
        c.push_group ();
        base.draw (c);
        c.pop_group_to_source ();
        c.paint_with_alpha (fade_tracker.alpha);
        return false;
    }
}