1 #include "urlgfe.h"
2 #include "category_view.h"
3 #include "setting_view_icons.h"
4
5 /* data set function for GtkCellRenderer */
col_set_icon(GtkTreeViewColumn * tree_column,GtkCellRenderer * cell,GtkTreeModel * model,GtkTreeIter * iter,gpointer data)6 static void col_set_icon (GtkTreeViewColumn *tree_column,
7 GtkCellRenderer *cell,
8 GtkTreeModel *model,
9 GtkTreeIter *iter,
10 gpointer data)
11 {
12 DownloadListView* download_view;
13 Category* category;
14 guint state;
15
16 gtk_tree_model_get (model, iter,
17 CATEGORY_COL_DATA, &category,
18 CATEGORY_COL_DOWNLOAD_VIEW, &download_view,
19 -1);
20
21 if (&category->completed_view == download_view)
22 state = CATEGORY_STATE_COMPLETED;
23 else if (&category->recycled_view == download_view)
24 state = CATEGORY_STATE_RECYCLED;
25 else
26 state = CATEGORY_SETTING(category)->state;
27
28 g_object_set ((GtkCellRenderer*)cell,
29 "pixbuf",
30 download_state_icons[state],
31 NULL);
32 }
33
col_set_name(GtkTreeViewColumn * tree_column,GtkCellRenderer * cell,GtkTreeModel * model,GtkTreeIter * iter,gpointer data)34 static void col_set_name (GtkTreeViewColumn *tree_column,
35 GtkCellRenderer *cell,
36 GtkTreeModel *model,
37 GtkTreeIter *iter,
38 gpointer data)
39 {
40 DownloadListView* download_view;
41 Category* cate;
42 gchar* name;
43
44 gtk_tree_model_get (model, iter,
45 CATEGORY_COL_DATA, &cate,
46 CATEGORY_COL_DOWNLOAD_VIEW, &download_view,
47 -1);
48
49 if (&cate->completed_view == download_view)
50 name = _("Completed");
51 else if (&cate->recycled_view == download_view)
52 name = _("Recycled");
53 else
54 name = cate->setting.name;
55
56 g_object_set ((GtkCellRenderer*)cell,
57 "text",
58 name,
59 NULL);
60 }
61
col_set_jobs(GtkTreeViewColumn * tree_column,GtkCellRenderer * cell,GtkTreeModel * model,GtkTreeIter * iter,gpointer data)62 static void col_set_jobs (GtkTreeViewColumn *tree_column,
63 GtkCellRenderer *cell,
64 GtkTreeModel *model,
65 GtkTreeIter *iter,
66 gpointer data)
67 {
68 gchar* str;
69 guint n_jobs;
70 DownloadStore* ds;
71
72 gtk_tree_model_get (model, iter,
73 CATEGORY_COL_DOWNLOAD_STORE, &ds,
74 -1);
75
76 n_jobs = gtk_tree_model_iter_n_children ((GtkTreeModel*)ds->list_store,
77 NULL);
78
79 str = g_strdup_printf ("%u", n_jobs);
80 g_object_set ((GtkCellRenderer*)cell,
81 "text",
82 str,
83 NULL);
84 g_free (str);
85 }
86
category_view_add_column_of_name(GtkTreeView * view)87 static void category_view_add_column_of_name(GtkTreeView* view)
88 {
89 GtkCellRenderer* renderer;
90 GtkTreeViewColumn* column;
91
92 column = gtk_tree_view_column_new ();
93 gtk_tree_view_column_set_title (column, _("Name"));
94 renderer = gtk_cell_renderer_pixbuf_new ();
95 gtk_tree_view_column_pack_start (column, renderer, FALSE);
96 gtk_tree_view_column_set_cell_data_func (column,
97 renderer,
98 col_set_icon,
99 NULL, NULL);
100 renderer = gtk_cell_renderer_text_new ();
101 gtk_tree_view_column_pack_start (column, renderer, TRUE);
102 gtk_tree_view_column_set_cell_data_func (column,
103 renderer,
104 col_set_name,
105 NULL, NULL);
106 gtk_tree_view_column_set_resizable (column, TRUE);
107 gtk_tree_view_append_column (view, column);
108 }
109
category_tree_view_new(CategoryStore * cs)110 GtkTreeView* category_tree_view_new (CategoryStore* cs)
111 {
112 GtkCellRenderer* renderer;
113 GtkTreeViewColumn* column;
114 GtkTreeView* view;
115
116 view = (GtkTreeView*)gtk_tree_view_new ();
117 /* render icons for GtkTreeView */
118 download_state_icons_render ((GtkWidget*)view);
119
120 gtk_tree_view_set_model (view,
121 GTK_TREE_MODEL(cs->tree_store));
122 // gtk_tree_view_set_rules_hint (view, TRUE);
123
124 /* add columns to GtkTreeView */
125
126 // column Category Name
127 category_view_add_column_of_name(view);
128
129 // column jobs
130 column = gtk_tree_view_column_new ();
131 gtk_tree_view_column_set_title (column, "Jobs");
132 renderer = gtk_cell_renderer_text_new ();
133 gtk_tree_view_column_pack_start (column, renderer, TRUE);
134 gtk_tree_view_column_set_cell_data_func (column,
135 renderer,
136 col_set_jobs,
137 NULL, NULL);
138 gtk_tree_view_column_set_resizable (column, TRUE);
139 gtk_tree_view_append_column (view, column);
140
141 gtk_widget_show_all ((GtkWidget*)view);
142
143 return view;
144 }
145
category_list_view_new(CategoryStore * cs)146 GtkTreeView* category_list_view_new (CategoryStore* cs)
147 {
148 GtkTreeView* view;
149
150 view = (GtkTreeView*)gtk_tree_view_new ();
151 /* render icons for GtkTreeView */
152 download_state_icons_render ((GtkWidget*)view);
153
154 gtk_tree_view_set_model (view,
155 GTK_TREE_MODEL(cs->list_store));
156
157 /* add columns to GtkTreeView */
158
159 // column Category Name
160 category_view_add_column_of_name(view);
161
162 gtk_widget_show_all ((GtkWidget*)view);
163
164 return view;
165 }
166
category_tree_view_get_selected(GtkTreeView * tree_view,Category ** category,DownloadListView ** download_view,DownloadStore ** download_store)167 gboolean category_tree_view_get_selected (GtkTreeView* tree_view,
168 Category** category,
169 DownloadListView** download_view,
170 DownloadStore** download_store)
171 {
172 GtkTreeIter iter;
173 GtkTreeModel* model;
174 GtkTreeSelection* selection;
175 gboolean had_selected;
176
177 Category* cate = NULL;
178 DownloadListView* down_view = NULL;
179 DownloadStore* down_store = NULL;
180
181 selection = gtk_tree_view_get_selection (tree_view);
182 had_selected = gtk_tree_selection_get_selected (selection,
183 &model,
184 &iter);
185 if (had_selected) {
186 gtk_tree_model_get (model,
187 &iter,
188 CATEGORY_COL_DATA,
189 &cate,
190 CATEGORY_COL_DOWNLOAD_VIEW,
191 &down_view,
192 CATEGORY_COL_DOWNLOAD_STORE,
193 &down_store,
194 -1);
195 }
196 if (category)
197 *category = cate;
198 if (download_view)
199 *download_view = down_view;
200 if (download_store)
201 *download_store = down_store;
202
203 return had_selected;
204 }
205
206