Source list view: add get_iter, get_selection and set_hildon_ui_mode methods
[cinaest] / src / source-list-view.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 Philipp Zabel
4  *
5  * Cinaest is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Cinaest is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Gtk;
20 using Hildon;
21
22 public class SourceListView : PannableArea {
23         TreeView tree;
24         ListStore store;
25
26         public signal void source_activated (MovieSource source);
27
28         public SourceListView (List<MovieSource> list) {
29                 // Source list
30                 store = new ListStore (3, typeof (string), typeof (string), typeof (MovieSource));
31                 add_sources (list);
32
33                 // Tree View
34                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
35                 tree.set_headers_visible (false);
36
37                 add (tree);
38
39                 tree.set_rules_hint (true);
40
41                 // Tree selection object
42                 var selection = tree.get_selection ();
43                 selection.set_mode (SelectionMode.SINGLE);
44
45                 // Source column
46                 var title_column = new TreeViewColumn ();
47                 title_column.set_title (_("Source"));
48                 title_column.set_reorderable (false);
49                 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
50                 title_column.set_expand (true);
51
52                 // Add name
53                 var vbox_renderer = new CellRendererVBox ();
54
55                 var renderer = new CellRendererText ();
56                 vbox_renderer.append (renderer, true);
57                 vbox_renderer.set_data ("name", renderer);
58
59                 // Add description
60                 renderer = new CellRendererText ();
61                 renderer.set ("ellipsize", Pango.EllipsizeMode.END);
62
63                 Pango.AttrList attr_list = new Pango.AttrList ();
64                 var style = Gtk.rc_get_style_by_paths (this.get_settings (), "SmallSystemFont", null, typeof (void));
65                 if (style != null) {
66                         var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
67                         attr_list.insert ((owned) attr_font_desc);
68                 } else {
69                         Pango.Attribute attr_font_scale = Pango.attr_scale_new (Pango.Scale.SMALL);
70                         attr_list.insert ((owned) attr_font_scale);
71                 }
72                 Gdk.Color color;
73                 if (!style.lookup_color ("SecondaryTextColor", out color)) {
74                         Gdk.Color.parse ("grey", out color);
75                 }
76                 Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
77                 attr_list.insert ((owned) attr_color);
78                 renderer.attributes = attr_list;
79
80                 vbox_renderer.append (renderer, true);
81                 vbox_renderer.set_data ("description", renderer);
82
83                 title_column.pack_start (vbox_renderer, true);
84                 title_column.set_cell_data_func (vbox_renderer, vbox_data_func);
85
86                 tree.append_column (title_column);
87
88                 // Connect signals
89                 tree.row_activated.connect (on_row_activated);
90         }
91
92         public void add_sources (List<MovieSource> list) {
93                 foreach (MovieSource source in list) {
94                         TreeIter iter;
95                         store.append (out iter);
96                         store.set (iter, 0, source.get_name (), 1, source.get_description (), 2, source);
97                 }
98         }
99
100         public bool get_iter (MovieSource source, out Gtk.TreeIter iter) {
101                 if (store.get_iter_first (out iter)) do {
102                         MovieSource s = null;
103                         store.get (iter, 2, out s);
104                         if (s == source) {
105                                 return true;
106                         }
107                 } while (store.iter_next (ref iter));
108
109                 return false;
110         }
111
112         public unowned TreeSelection get_selection () {
113                 return tree.get_selection ();
114         }
115
116         public void set_hildon_ui_mode (UIMode mode) {
117                 var selection = tree.get_selection ();
118
119                 if (mode == UIMode.NORMAL) {
120                         selection.set_mode (SelectionMode.NONE);
121                 }
122                 Hildon.gtk_tree_view_set_ui_mode (tree, mode);
123                 if (mode == UIMode.EDIT) {
124                         selection.set_mode (SelectionMode.MULTIPLE);
125                 }
126         }
127
128         private void on_row_activated (TreeView tree, TreePath path_, TreeViewColumn column) {
129                 TreePath path = path_.copy (); // FIXME - calling model.get_iter with path_ directly causes a C qualifier warning
130                 TreeModel model = tree.model;
131                 TreeIter iter;
132
133                 if (model.get_iter (out iter, path)) {
134                         MovieSource source;
135                         model.get (iter, 2, out source);
136                         source_activated (source);
137                 }
138         }
139
140         private void vbox_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
141                 CellRendererText renderer;
142                 string name;
143                 string description;
144
145                 model.get (iter, 0, out name, 1, out description);
146
147                 renderer = (CellRendererText) cell.get_data ("name");
148                 renderer.text = name;
149
150                 renderer = (CellRendererText) cell.get_data ("description");
151                 renderer.text = description;
152         }
153 }