Add movie list view
[cinaest] / src / movie-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 MovieListView : PannableArea {
23         private enum Columns {
24                 TITLE,
25                 YEAR,
26                 RATING,
27                 N_COLUMNS
28         }
29
30         ListStore store;
31         TreeView tree;
32         public TreeSortable sorted_store;
33
34         construct {
35                 store = new ListStore (3, typeof (string), typeof (int), typeof (int));
36
37                 // FIXME - fill store with test data
38                 int i;
39                 for (i = 0; i < 10; i++) {
40                         TreeIter iter;
41
42                         store.append (out iter);
43                         store.set_value (iter, Columns.TITLE, "Test %d".printf (i));
44                         store.set_value (iter, Columns.YEAR, 2000 - i);
45                         store.set_value (iter, Columns.RATING, 10 * (i % 5) + i);
46                 }
47
48                 // Add filter wrapper
49                 var filtered_store = new TreeModelFilter (store, null);
50
51                 // Add sort wrapper
52                 sorted_store = new TreeModelSort.with_model (filtered_store);
53
54                 // Tree View
55                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, sorted_store);
56                 tree.set_headers_visible (false);
57
58                 add (tree);
59
60                 tree.set_rules_hint (true);
61
62                 // Tree selection object
63                 var selection = tree.get_selection ();
64                 selection.set_mode (SelectionMode.SINGLE);
65
66                 // Title column
67                 var title_column = new TreeViewColumn ();
68                 title_column.set_title ("Movie");
69                 title_column.set_sort_column_id (Columns.TITLE);
70                 title_column.set_reorderable (false);
71                 title_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
72                 title_column.set_expand (true);
73
74                 // Add text to column
75                 var renderer = new CellRendererText ();
76                 renderer.set ("ellipsize", Pango.EllipsizeMode.END);
77                 title_column.pack_start (renderer, true);
78                 title_column.add_attribute (renderer, "text", Columns.TITLE);
79                 tree.append_column (title_column);
80
81                 // Sort by title
82                 sorted_store.set_sort_column_id (Columns.TITLE, SortType.ASCENDING);
83
84                 // Year column
85                 renderer = new CellRendererText ();
86                 var year_column = new TreeViewColumn.with_attributes ("Year", renderer, "text", Columns.YEAR);
87                 year_column.set_sort_column_id (Columns.YEAR);
88                 year_column.set_reorderable (false);
89                 year_column.set_sort_order (SortType.DESCENDING);
90                 tree.append_column (year_column);
91
92                 // Rating column
93                 renderer = new CellRendererText ();
94                 var rating_column = new TreeViewColumn.with_attributes ("Rating", renderer, "text", Columns.RATING);
95                 rating_column.set_sort_column_id (Columns.RATING);
96                 rating_column.set_reorderable (false);
97                 rating_column.set_sort_order (SortType.DESCENDING);
98                 rating_column.set_cell_data_func (renderer, rating_data_func);
99                 rating_column.xalign = (float) 1.0;
100                 tree.append_column (rating_column);
101         }
102
103         private void rating_data_func (CellLayout cell_layout, CellRenderer cell, TreeModel model, TreeIter iter) {
104                 int rating;
105
106                 model.get (iter, MovieListStore.Columns.RATING, out rating);
107                 ((CellRendererText) cell).text = "%d.%d".printf (rating / 10, rating % 10);
108         }
109 }