~nonamenoname/slingshot/fix-1084101

« back to all changes in this revision

Viewing changes to src/Widgets/Sidebar.vala

  • Committer: RabbitBot
  • Author(s): Corentin Noël
  • Date: 2014-01-14 13:05:03 UTC
  • mfrom: (396.1.1 slingshot)
  • Revision ID: rabbitbot-20140114130503-vl01yvhtdlu0dvs4
* Removed every using statement
* Changed VWidgets and HWidgets to Widgets with the corresponding orientation (Gtk deprecation)
* Ported Zeitgeist parts to version 2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
17
//
18
18
 
19
 
using Gtk;
20
 
 
21
 
namespace Slingshot.Widgets {
22
 
 
23
 
    public class Sidebar : TreeView {
24
 
 
25
 
        private TreeStore store;
26
 
 
27
 
        private TreeIter entry_iter;
28
 
 
29
 
        public int cat_size {
30
 
            get {
31
 
                return store.iter_n_children (null);
32
 
            }
33
 
        }
34
 
 
35
 
        private int _selected;
36
 
        public int selected {
37
 
            get {
38
 
                return _selected;
39
 
            }
40
 
            set {
41
 
                if (value >= 0 && value < cat_size) {
42
 
                    select_nth (value);
43
 
                    _selected = value;
44
 
                }
45
 
            }
46
 
        }
47
 
 
48
 
        private enum Columns {
49
 
            INT,
50
 
            TEXT,
51
 
            N_COLUMNS
52
 
        }
53
 
 
54
 
        public signal void selection_changed (string entry_name, int nth);
55
 
 
56
 
        public Sidebar () {
57
 
 
58
 
            store = new TreeStore (Columns.N_COLUMNS, typeof (int), typeof (string));
59
 
            store.set_sort_column_id (1, Gtk.SortType.ASCENDING);
60
 
            set_model (store);
61
 
 
62
 
            set_headers_visible (false);
63
 
            set_show_expanders (false);
64
 
            set_level_indentation (8);
65
 
 
66
 
            set_size_request (145, -1);
67
 
            get_style_context ().add_class ("sidebar");
68
 
 
69
 
            var cell = new CellRendererText ();
70
 
            cell.wrap_mode = Pango.WrapMode.WORD;
71
 
            cell.wrap_width = 110;
72
 
            cell.xpad = 17;
73
 
 
74
 
            insert_column_with_attributes (-1, "Filters", cell, "markup", Columns.TEXT);
75
 
 
76
 
            get_selection ().set_mode (SelectionMode.SINGLE);
77
 
            get_selection ().changed.connect (selection_change);
78
 
 
79
 
        }
80
 
 
81
 
        public void add_category (string entry_name) {
82
 
 
83
 
            store.append (out entry_iter, null);
84
 
            store.set (entry_iter, Columns.INT, cat_size - 1, Columns.TEXT, Markup.escape_text (entry_name), -1);
85
 
 
86
 
            expand_all ();
87
 
 
88
 
        }
89
 
 
90
 
        public void selection_change () {
91
 
 
92
 
            TreeModel model;
93
 
            TreeIter sel_iter;
94
 
            string name;
95
 
            int nth;
96
 
 
97
 
            if (get_selection ().get_selected (out model, out sel_iter)) {
98
 
                store.get (sel_iter, Columns.INT, out nth, Columns.TEXT, out name);
99
 
                _selected = nth;
100
 
                selection_changed (name, nth);
101
 
            }
102
 
 
103
 
        }
104
 
 
105
 
        public bool select_nth (int nth) {
106
 
 
107
 
            TreeIter iter;
108
 
 
109
 
            if (nth < cat_size)
110
 
                store.iter_nth_child (out iter, null, nth);
111
 
            else
112
 
                return false;
113
 
 
114
 
            get_selection ().select_iter (iter);
115
 
            return true;
116
 
 
117
 
        }
118
 
 
119
 
        protected override bool scroll_event (Gdk.EventScroll event) {
120
 
 
121
 
            switch (event.direction.to_string ()) {
122
 
                case "GDK_SCROLL_UP":
123
 
                case "GDK_SCROLL_LEFT":
124
 
                    selected--;
125
 
                    break;
126
 
                case "GDK_SCROLL_DOWN":
127
 
                case "GDK_SCROLL_RIGHT":
128
 
                    selected++;
129
 
                    break;
130
 
 
131
 
            }
132
 
 
 
19
public class Slingshot.Widgets.Sidebar : Gtk.TreeView {
 
20
 
 
21
    private Gtk.TreeStore store;
 
22
 
 
23
    private Gtk.TreeIter entry_iter;
 
24
 
 
25
    public int cat_size {
 
26
        get {
 
27
            return store.iter_n_children (null);
 
28
        }
 
29
    }
 
30
 
 
31
    private int _selected;
 
32
    public int selected {
 
33
        get {
 
34
            return _selected;
 
35
        }
 
36
        set {
 
37
            if (value >= 0 && value < cat_size) {
 
38
                select_nth (value);
 
39
                _selected = value;
 
40
            }
 
41
        }
 
42
    }
 
43
 
 
44
    private enum Columns {
 
45
        INT,
 
46
        TEXT,
 
47
        N_COLUMNS
 
48
    }
 
49
 
 
50
    public signal void selection_changed (string entry_name, int nth);
 
51
 
 
52
    public Sidebar () {
 
53
 
 
54
        store = new Gtk.TreeStore (Columns.N_COLUMNS, typeof (int), typeof (string));
 
55
        store.set_sort_column_id (1, Gtk.SortType.ASCENDING);
 
56
        set_model (store);
 
57
 
 
58
        set_headers_visible (false);
 
59
        set_show_expanders (false);
 
60
        set_level_indentation (8);
 
61
 
 
62
        set_size_request (145, -1);
 
63
        get_style_context ().add_class ("sidebar");
 
64
 
 
65
        var cell = new Gtk.CellRendererText ();
 
66
        cell.wrap_mode = Pango.WrapMode.WORD;
 
67
        cell.wrap_width = 110;
 
68
        cell.xpad = 17;
 
69
 
 
70
        insert_column_with_attributes (-1, "Filters", cell, "markup", Columns.TEXT);
 
71
 
 
72
        get_selection ().set_mode (Gtk.SelectionMode.SINGLE);
 
73
        get_selection ().changed.connect (selection_change);
 
74
 
 
75
    }
 
76
 
 
77
    public void add_category (string entry_name) {
 
78
 
 
79
        store.append (out entry_iter, null);
 
80
        store.set (entry_iter, Columns.INT, cat_size - 1, Columns.TEXT, Markup.escape_text (entry_name), -1);
 
81
 
 
82
        expand_all ();
 
83
 
 
84
    }
 
85
 
 
86
    public void selection_change () {
 
87
 
 
88
        Gtk.TreeModel model;
 
89
        Gtk.TreeIter sel_iter;
 
90
        string name;
 
91
        int nth;
 
92
 
 
93
        if (get_selection ().get_selected (out model, out sel_iter)) {
 
94
            store.get (sel_iter, Columns.INT, out nth, Columns.TEXT, out name);
 
95
            _selected = nth;
 
96
            selection_changed (name, nth);
 
97
        }
 
98
 
 
99
    }
 
100
 
 
101
    public bool select_nth (int nth) {
 
102
 
 
103
        Gtk.TreeIter iter;
 
104
 
 
105
        if (nth < cat_size)
 
106
            store.iter_nth_child (out iter, null, nth);
 
107
        else
133
108
            return false;
134
109
 
 
110
        get_selection ().select_iter (iter);
 
111
        return true;
 
112
 
 
113
    }
 
114
 
 
115
    protected override bool scroll_event (Gdk.EventScroll event) {
 
116
 
 
117
        switch (event.direction.to_string ()) {
 
118
            case "GDK_SCROLL_UP":
 
119
            case "GDK_SCROLL_LEFT":
 
120
                selected--;
 
121
                break;
 
122
            case "GDK_SCROLL_DOWN":
 
123
            case "GDK_SCROLL_RIGHT":
 
124
                selected++;
 
125
                break;
 
126
 
135
127
        }
136
128
 
 
129
        return false;
 
130
 
137
131
    }
138
132
 
139
133
}