1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
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.
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.
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/>.
22 public class SourceListView : PannableArea {
25 TreeModelFilter filtered_store;
28 public signal void source_activated (MovieSource source);
30 public SourceListView (List<MovieSource> list, bool _filtered) {
34 store = new ListStore (3, typeof (string), typeof (string), typeof (MovieSource));
39 filtered_store = new TreeModelFilter (store, null);
40 filtered_store.set_visible_func (filter_visible_func);
43 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, filtered_store);
45 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
47 tree.set_headers_visible (false);
51 tree.set_rules_hint (true);
53 // Tree selection object
54 var selection = tree.get_selection ();
55 selection.set_mode (SelectionMode.SINGLE);
58 var title_column = new TreeViewColumn ();
59 title_column.set_title (_("Source"));
60 title_column.set_reorderable (false);
61 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
62 title_column.set_expand (true);
65 var vbox_renderer = new CellRendererVBox ();
67 var renderer = new CellRendererText ();
68 vbox_renderer.append (renderer, true);
69 vbox_renderer.set_data ("name", renderer);
72 renderer = new CellRendererText ();
73 renderer.set ("ellipsize", Pango.EllipsizeMode.END);
75 Pango.AttrList attr_list = new Pango.AttrList ();
76 var style = Gtk.rc_get_style_by_paths (this.get_settings (), "SmallSystemFont", null, typeof (void));
78 var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
79 attr_list.insert ((owned) attr_font_desc);
81 Pango.Attribute attr_font_scale = Pango.attr_scale_new (Pango.Scale.SMALL);
82 attr_list.insert ((owned) attr_font_scale);
85 if (!style.lookup_color ("SecondaryTextColor", out color)) {
86 Gdk.Color.parse ("grey", out color);
88 Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
89 attr_list.insert ((owned) attr_color);
90 renderer.attributes = attr_list;
92 vbox_renderer.append (renderer, true);
93 vbox_renderer.set_data ("description", renderer);
95 title_column.pack_start (vbox_renderer, true);
96 title_column.set_cell_data_func (vbox_renderer, vbox_data_func);
98 tree.append_column (title_column);
101 tree.row_activated.connect (on_row_activated);
104 bool filter_visible_func (TreeModel model, TreeIter iter) {
105 MovieSource source = null;
107 store.get (iter, 2, out source);
109 return source.active;
112 public void add_sources (List<MovieSource> list) {
113 foreach (MovieSource source in list) {
116 store.insert_with_values (out iter, -1, 0, source.get_name (), 1, source.get_description (), 2, source);
118 source.notify["active"].connect (this.on_source_changed);
122 private void on_source_changed (GLib.Object _source, GLib.ParamSpec spec) {
123 var source = (MovieSource) _source;
126 if (get_iter (source, out iter)) {
127 TreePath path = store.get_path (iter);
128 store.row_changed (path, iter);
132 public bool get_iter (MovieSource source, out Gtk.TreeIter iter) {
133 if (store.get_iter_first (out iter)) do {
134 MovieSource s = null;
135 store.get (iter, 2, out s);
139 } while (store.iter_next (ref iter));
144 public unowned TreeSelection get_selection () {
145 return tree.get_selection ();
148 public void set_hildon_ui_mode (UIMode mode) {
149 var selection = tree.get_selection ();
151 if (mode == UIMode.NORMAL) {
152 selection.set_mode (SelectionMode.NONE);
154 Hildon.gtk_tree_view_set_ui_mode (tree, mode);
155 if (mode == UIMode.EDIT) {
156 selection.set_mode (SelectionMode.MULTIPLE);
160 public int length () {
164 if (store.get_iter_first (out iter)) do {
166 } while (store.iter_next (ref iter));
171 private void on_row_activated (TreeView tree, TreePath path_, TreeViewColumn column) {
172 TreePath path = path_.copy (); // FIXME - calling model.get_iter with path_ directly causes a C qualifier warning
173 TreeModel model = tree.model;
176 if (model.get_iter (out iter, path)) {
178 model.get (iter, 2, out source);
179 source_activated (source);
183 private void vbox_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
184 CellRendererText renderer;
188 model.get (iter, 0, out name, 1, out description);
190 renderer = cell.get_data ("name");
191 renderer.text = name;
193 renderer = cell.get_data ("description");
194 renderer.text = description;