Add source list view
[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
25         public signal void source_activated (MovieSource source);
26
27         public SourceListView (List<MovieSource> list) {
28                 // Source list
29                 var store = new ListStore (3, typeof (string), typeof (string), typeof (MovieSource));
30                 foreach (MovieSource source in list) {
31                         TreeIter iter;
32                         store.append (out iter);
33                         store.set (iter, 0, source.get_name (), 1, source.get_description (), 2, source);
34                 }
35
36                 // Tree View
37                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
38                 tree.set_headers_visible (false);
39
40                 add (tree);
41
42                 tree.set_rules_hint (true);
43
44                 // Tree selection object
45                 var selection = tree.get_selection ();
46                 selection.set_mode (SelectionMode.SINGLE);
47
48                 // Source column
49                 var title_column = new TreeViewColumn ();
50                 title_column.set_title (_("Source"));
51                 title_column.set_reorderable (false);
52                 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
53                 title_column.set_expand (true);
54
55                 // Add name
56                 var vbox_renderer = new CellRendererVBox ();
57
58                 var renderer = new CellRendererText ();
59                 vbox_renderer.append (renderer, true);
60                 vbox_renderer.set_data ("name", renderer);
61
62                 // Add description
63                 renderer = new CellRendererText ();
64                 renderer.set ("ellipsize", Pango.EllipsizeMode.END);
65
66                 Pango.AttrList attr_list = new Pango.AttrList ();
67                 var style = Gtk.rc_get_style_by_paths (this.get_settings (), "SmallSystemFont", null, typeof (void));
68                 if (style != null) {
69                         Pango.Attribute attr_font_desc = Pango.attr_font_desc_new (style.font_desc.copy ());
70                         attr_list.insert ((owned) attr_font_desc);
71                 } else {
72                         Pango.Attribute attr_font_scale = Pango.attr_scale_new (Pango.Scale.SMALL);
73                         attr_list.insert ((owned) attr_font_scale);
74                 }
75                 Gdk.Color color;
76                 if (!style.lookup_color ("SecondaryTextColor", out color)) {
77                         Gdk.Color.parse ("grey", out color);
78                 }
79                 Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
80                 attr_list.insert ((owned) attr_color);
81                 renderer.attributes = attr_list;
82
83                 vbox_renderer.append (renderer, true);
84                 vbox_renderer.set_data ("description", renderer);
85
86                 title_column.pack_start (vbox_renderer, true);
87                 title_column.set_cell_data_func (vbox_renderer, vbox_data_func);
88
89                 tree.append_column (title_column);
90
91                 // Connect signals
92                 tree.row_activated.connect (on_row_activated);
93         }
94
95         private void on_row_activated (TreeView tree, TreePath path_, TreeViewColumn column) {
96                 TreePath path = path_.copy (); // FIXME - calling model.get_iter with path_ directly causes a C qualifier warning
97                 TreeModel model = tree.model;
98                 TreeIter iter;
99
100                 if (model.get_iter (out iter, path)) {
101                         MovieSource source;
102                         model.get (iter, 2, out source);
103                         source_activated (source);
104                 }
105         }
106
107         private void vbox_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
108                 CellRendererText renderer;
109                 string name;
110                 string description;
111
112                 model.get (iter, 0, out name, 1, out description);
113
114                 renderer = (CellRendererText) cell.get_data ("name");
115                 renderer.text = name;
116
117                 renderer = (CellRendererText) cell.get_data ("description");
118                 renderer.text = description;
119         }
120 }