aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/util/label_hybrid.vala
blob: 4486f25b013e9ab23803a5fefd93b53ac0d594ce (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Gee;
using Gtk;

namespace Dino.Ui.Util {

public class LabelHybrid : Stack {

    public Label label = new Label("") { visible=true };
    protected Button button = new Button() { relief=ReliefStyle.NONE, visible=true };

    public void init(Widget widget) {
        button.add(label);
        add_named(button, "label");
        add_named(widget, "widget");

        button.clicked.connect(() => {
            show_widget();
        });
    }

    public void show_widget() {
        visible_child_name = "widget";
    }

    public void show_label() {
        visible_child_name = "label";
    }
}

public class EntryLabelHybrid : LabelHybrid {

    public string text {
        get { return entry.text; }
        set {
            entry.text = value;
            label.label = value;
        }
    }

    public bool visibility {
        get { return entry.visibility; }
        set { entry.visibility = value; }
    }

    public float xalign {
        get { return label.xalign; }
        set {
            label.xalign = value;
            entry.set_alignment(value);
        }
    }

    private Entry entry;

    public EntryLabelHybrid(Entry? e = null) {
        entry = e ?? new Entry() { visible=true };
        init(entry);
        update_label();

        entry.key_release_event.connect((event) => {
            if (event.keyval == Gdk.Key.Return) {
                show_label();
            } else {
                label.label = entry.text;
            }
            return false;
        });
    }

    private void update_label() {
        text = text;
    }
}

public class ComboBoxTextLabelHybrid : LabelHybrid {

    public int active {
        get { return combobox.active; }
        set { combobox.active = value; }
    }

    public float xalign {
        get { return label.xalign; }
        set { label.xalign = value; }
    }

    private ComboBoxText combobox;

    public ComboBoxTextLabelHybrid(ComboBoxText? cb = null) {
        combobox = cb ?? new ComboBoxText() { visible=true };
        init(combobox);
        update_label();

        combobox.changed.connect(() => {
            update_label();
            show_label();
        });
        button.clicked.connect(() => {
            combobox.popup();
        });
    }

    public void append(string id, string text) { combobox.append(id, text); }
    public string get_active_text() { return combobox.get_active_text(); }

    private void update_label() {
        label.label = combobox.get_active_text();
    }
}

public class LabelHybridGroup {

    private Gee.List<LabelHybrid> hybrids = new ArrayList<LabelHybrid>();

    public void add(LabelHybrid hybrid) {
        hybrids.add(hybrid);

        hybrid.notify["visible-child-name"].connect(() => {
            if (hybrid.visible_child_name == "label") return;
            foreach (LabelHybrid h in hybrids) {
                if (h != hybrid) {
                    h.set_visible_child_name("label");
                }
            }
        });
    }
}

}