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, Gtk.Window window) {
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 renderer.yalign = 1.0f;
69 vbox_renderer.append (renderer, true);
70 vbox_renderer.set_data ("name", renderer);
73 renderer = new CellRendererText ();
75 renderer.ellipsize = Pango.EllipsizeMode.END;
77 Pango.AttrList attr_list = new Pango.AttrList ();
78 var style = Gtk.rc_get_style_by_paths (Gtk.Settings.get_default (), "SmallSystemFont", null, typeof (void));
80 var attr_font_desc = new Pango.AttrFontDesc (style.font_desc.copy ());
81 attr_list.insert ((owned) attr_font_desc);
84 window.ensure_style ();
85 if (window.style.lookup_color ("SecondaryTextColor", out color)) {
86 Pango.Attribute attr_color = Pango.attr_foreground_new (color.red, color.green, color.blue);
87 attr_list.insert ((owned) attr_color);
89 renderer.attributes = attr_list;
91 vbox_renderer.append (renderer, true);
92 vbox_renderer.set_data ("description", renderer);
94 title_column.pack_start (vbox_renderer, true);
95 title_column.set_cell_data_func (vbox_renderer, vbox_data_func);
97 tree.append_column (title_column);
100 tree.row_activated.connect (on_row_activated);
103 bool filter_visible_func (TreeModel model, TreeIter iter) {
104 MovieSource source = null;
106 store.get (iter, 2, out source);
108 return source.active;
111 public void add_sources (List<MovieSource> list) {
112 foreach (MovieSource source in list) {
115 store.insert_with_values (out iter, -1, 0, source.get_name (), 1, source.get_description (), 2, source);
117 source.notify["active"].connect (this.on_source_changed);
121 private void on_source_changed (GLib.Object _source, GLib.ParamSpec spec) {
122 var source = (MovieSource) _source;
125 if (get_iter (source, out iter)) {
126 TreePath path = store.get_path (iter);
127 store.row_changed (path, iter);
131 public bool get_iter (MovieSource source, out Gtk.TreeIter iter) {
132 if (store.get_iter_first (out iter)) do {
133 MovieSource s = null;
134 store.get (iter, 2, out s);
138 } while (store.iter_next (ref iter));
143 public unowned TreeSelection get_selection () {
144 return tree.get_selection ();
147 public void set_hildon_ui_mode (UIMode mode) {
148 var selection = tree.get_selection ();
150 if (mode == UIMode.NORMAL) {
151 selection.set_mode (SelectionMode.NONE);
153 Hildon.gtk_tree_view_set_ui_mode (tree, mode);
154 if (mode == UIMode.EDIT) {
155 selection.set_mode (SelectionMode.MULTIPLE);
159 public int length () {
163 if (store.get_iter_first (out iter)) do {
165 } while (store.iter_next (ref iter));
170 private void on_row_activated (TreeView tree, TreePath path_, TreeViewColumn column) {
171 TreePath path = path_.copy (); // FIXME - calling model.get_iter with path_ directly causes a C qualifier warning
172 TreeModel model = tree.model;
175 if (model.get_iter (out iter, path)) {
177 model.get (iter, 2, out source);
178 source_activated (source);
182 private void vbox_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
183 CellRendererText renderer;
187 model.get (iter, 0, out name, 1, out description);
189 renderer = cell.get_data ("name");
190 renderer.text = name;
192 renderer = cell.get_data ("description");
193 renderer.text = description;