~ubuntu-branches/ubuntu/utopic/unity-greeter/utopic

« back to all changes in this revision

Viewing changes to src/toggle-box.vala

  • Committer: Package Import Robot
  • Author(s): Michael Terry
  • Date: 2013-01-04 10:37:29 UTC
  • mto: This revision was merged to the branch mainline in revision 51.
  • Revision ID: package-import@ubuntu.com-20130104103729-ydal31wfuhirb8zb
Tags: upstream-13.04.0
ImportĀ upstreamĀ versionĀ 13.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
 *
 
3
 * Copyright (C) 2012 Canonical Ltd
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License version 3 as
 
7
 * published by the Free Software Foundation.
 
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, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Authors: Michael Terry <michael.terry@canonical.com>
 
18
 */
 
19
 
 
20
public class ToggleBox : Gtk.Box
 
21
{
 
22
    public string default_key {get; construct;}
 
23
    public string starting_key {get; construct;}
 
24
    public string selected_key {get; protected set;}
 
25
 
 
26
    public ToggleBox (string? default_key, string? starting_key)
 
27
    {
 
28
        Object (default_key: default_key, starting_key: starting_key,
 
29
                selected_key: starting_key);
 
30
    }
 
31
 
 
32
    public void add_item (string key, string label, Gdk.Pixbuf? icon)
 
33
    {
 
34
        var item = make_button (key, label, icon);
 
35
 
 
36
        if (get_children () == null ||
 
37
            (starting_key == null && default_key == key) ||
 
38
            starting_key == key)
 
39
            select (item);
 
40
 
 
41
        item.show ();
 
42
        add (item);
 
43
    }
 
44
 
 
45
    private Gtk.Button selected_button;
 
46
 
 
47
    construct
 
48
    {
 
49
        orientation = Gtk.Orientation.VERTICAL;
 
50
    }
 
51
 
 
52
    public override bool draw (Cairo.Context c)
 
53
    {
 
54
        Gtk.Allocation allocation;
 
55
        get_allocation (out allocation);
 
56
 
 
57
        DashBox.cairo_rounded_rectangle (c, 0, 0, allocation.width,
 
58
                                         allocation.height, 0.1 * grid_size);
 
59
        c.set_source_rgba (0.5, 0.5, 0.5, 0.5);
 
60
        c.set_line_width (1);
 
61
        c.stroke ();
 
62
 
 
63
        return base.draw (c);
 
64
    }
 
65
 
 
66
    private void select (Gtk.Button button)
 
67
    {
 
68
        if (selected_button != null)
 
69
            selected_button.relief = Gtk.ReliefStyle.NONE;
 
70
        selected_button = button;
 
71
        selected_button.relief = Gtk.ReliefStyle.NORMAL;
 
72
        selected_key = selected_button.get_data<string> ("toggle-list-key");
 
73
    }
 
74
 
 
75
    private Gtk.Button make_button (string key, string name_in, Gdk.Pixbuf? icon)
 
76
    {
 
77
        var item = new FlatButton ();
 
78
        item.relief = Gtk.ReliefStyle.NONE;
 
79
        item.clicked.connect (button_clicked_cb);
 
80
 
 
81
        var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
 
82
 
 
83
        if (icon != null)
 
84
        {
 
85
            var image = new CachedImage (icon);
 
86
            hbox.pack_start (image, false, false, 0);
 
87
        }
 
88
 
 
89
        var name = name_in;
 
90
        if (key == default_key)
 
91
        {
 
92
            /* Translators: %s is a session name like KDE or Ubuntu */
 
93
            name = _("%s (Default)").printf (name);
 
94
        }
 
95
 
 
96
        var label = new Gtk.Label (null);
 
97
        label.set_markup ("<span font=\"Ubuntu 13\">%s</span>".printf (name));
 
98
        label.halign = Gtk.Align.START;
 
99
        hbox.pack_start (label, true, true, 0);
 
100
 
 
101
        item.hexpand = true;
 
102
        item.add (hbox);
 
103
        hbox.show_all ();
 
104
 
 
105
        try
 
106
        {
 
107
            /* Tighten padding on buttons to not be so large */
 
108
            var style = new Gtk.CssProvider ();
 
109
            style.load_from_data ("* {padding: 8px;}", -1);
 
110
            item.get_style_context ().add_provider (style, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
 
111
        }
 
112
        catch (Error e)
 
113
        {
 
114
            debug ("Internal error loading session chooser style: %s", e.message);
 
115
        }
 
116
 
 
117
        item.set_data<string> ("toggle-list-key", key);
 
118
        return item;
 
119
    }
 
120
 
 
121
    private void button_clicked_cb (Gtk.Button button)
 
122
    {
 
123
        selected_key = button.get_data<string> ("toggle-list-key");
 
124
    }
 
125
 
 
126
    public override void grab_focus ()
 
127
    {
 
128
        if (selected_button != null)
 
129
            selected_button.grab_focus ();
 
130
    }
 
131
}