comparison src/streambrowser/gui/streambrowser_win.c @ 2735:6d6a3eb67510

some work on the streambrowser
author Calin Crisan ccrisan@gmail.com
date Tue, 01 Jul 2008 02:05:25 +0300
parents
children 4ec0e13208de
comparison
equal deleted inserted replaced
2727:c68fadbad6d8 2735:6d6a3eb67510
1
2 #include <string.h>
3 #include <glib.h>
4 #include <gtk/gtk.h>
5
6 #include "../streambrowser.h"
7 #include "streambrowser_win.h"
8
9
10 typedef struct {
11
12 streamdir_t* streamdir;
13 GtkWidget* table;
14 GtkWidget* tree_view;
15
16 } streamdir_gui_t;
17
18
19 void (* update_function) (streamdir_t *streamdir, category_t *category, streaminfo_t *streaminfo);
20
21 static GtkWidget* gtk_label_new_with_icon(gchar *icon_filename, gchar *label_text);
22 static GtkWidget* gtk_streamdir_tree_view_new();
23 static GtkWidget* gtk_streamdir_table_new(GtkWidget *tree_view);
24
25 static gboolean on_notebook_switch_page(GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer data);
26 static gboolean on_tree_view_cursor_changed(GtkTreeView *tree_view, gpointer data);
27 static gboolean on_add_button_clicked(GtkButton *button, gpointer data);
28
29 static streamdir_gui_t* find_streamdir_gui_by_name(gchar *name);
30 static streamdir_gui_t* find_streamdir_gui_by_tree_view(GtkTreeView *tree_view);
31 static streamdir_gui_t* find_streamdir_gui_by_table(GtkTable *table);
32
33
34 static GtkWidget* notebook;
35 static GtkWidget* search_entry;
36 static GtkWidget* add_button;
37 static GtkWidget* streambrowser_window;
38 static GList* streamdir_gui_list;
39 static GtkCellRenderer* cell_renderer_pixbuf;
40 static GtkCellRenderer* cell_renderer_text;
41
42
43 void streambrowser_win_init()
44 {
45 /* notebook */
46 notebook = gtk_notebook_new();
47 g_signal_connect(G_OBJECT(notebook), "switch-page", G_CALLBACK(on_notebook_switch_page), NULL);
48 gtk_widget_show(notebook);
49
50 GtkWidget *search_label = gtk_label_new("Search:");
51 gtk_widget_show(search_label);
52
53 /* search entry */
54 search_entry = gtk_entry_new();
55 gtk_widget_show(search_entry);
56
57 GtkWidget *hbox1 = gtk_hbox_new(FALSE, 0);
58 gtk_box_pack_start(GTK_BOX(hbox1), search_label, FALSE, TRUE, 3);
59 gtk_box_pack_start(GTK_BOX(hbox1), search_entry, TRUE, TRUE, 3);
60 gtk_widget_show(hbox1);
61
62 /* add button */
63 add_button = gtk_button_new_from_stock(GTK_STOCK_ADD);
64 g_signal_connect(G_OBJECT(add_button), "clicked", G_CALLBACK(on_add_button_clicked), NULL);
65 gtk_widget_show(add_button);
66
67 GtkWidget *vbox1 = gtk_vbox_new(FALSE, 3);
68 gtk_box_pack_start(GTK_BOX(vbox1), notebook, TRUE, TRUE, 0);
69 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
70 gtk_box_pack_start(GTK_BOX(vbox1), add_button, FALSE, TRUE, 0);
71 gtk_widget_show(vbox1);
72
73 /* streambrowser window */
74 streambrowser_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
75 gtk_window_set_title(GTK_WINDOW(streambrowser_window), "Stream browser");
76 gtk_window_set_position(GTK_WINDOW(streambrowser_window), GTK_WIN_POS_CENTER);
77 gtk_window_set_default_size(GTK_WINDOW(streambrowser_window), 700, 400);
78 g_signal_connect(G_OBJECT(streambrowser_window), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), streambrowser_window);
79 gtk_container_add(GTK_CONTAINER(streambrowser_window), vbox1);
80
81 /* others */
82 cell_renderer_pixbuf = gtk_cell_renderer_pixbuf_new();
83 g_object_set(G_OBJECT(cell_renderer_pixbuf), "stock-id", "gtk-directory", NULL);
84 cell_renderer_text = gtk_cell_renderer_text_new();
85 }
86
87 void streambrowser_win_done()
88 {
89 }
90
91 void streambrowser_win_show()
92 {
93 gtk_widget_show(streambrowser_window);
94 }
95
96 void streambrowser_win_hide()
97 {
98 gtk_widget_hide(streambrowser_window);
99 }
100
101 void streambrowser_win_set_streamdir(streamdir_t *streamdir, gchar *icon_filename)
102 {
103 GtkWidget *tree_view = NULL;
104
105 /* search for an old instance of this streamdir and replace it */
106 streamdir_gui_t *streamdir_gui = find_streamdir_gui_by_name(streamdir->name);
107 if (streamdir_gui != NULL) {
108 streamdir_delete(streamdir_gui->streamdir);
109 streamdir_gui->streamdir = streamdir;
110 tree_view = streamdir_gui->tree_view;
111 }
112 /* if no older instance of this streamdir has been found, we add a brand new one */
113 else {
114 streamdir_gui = g_malloc(sizeof(streamdir_gui_t));
115
116 tree_view = gtk_streamdir_tree_view_new();
117
118 GtkWidget *table = gtk_streamdir_table_new(tree_view);
119 gtk_widget_show_all(table);
120
121 GtkWidget *label = gtk_label_new_with_icon(icon_filename, streamdir->name);
122 gtk_widget_show_all(label);
123
124 streamdir_gui->streamdir = streamdir;
125 streamdir_gui->tree_view = tree_view;
126 streamdir_gui->table = table;
127
128 streamdir_gui_list = g_list_append(streamdir_gui_list, streamdir_gui);
129
130 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), table, label);
131 }
132
133 /* fill the tree with categories */
134 GtkTreeIter iter;
135 GtkTreeStore *store = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(tree_view)));
136
137 gtk_tree_store_clear(store);
138
139 int i, count = category_get_count(streamdir);
140 category_t *category;
141 for (i = 0; i < count; i++) {
142 category = category_get_by_index(streamdir, i);
143
144 gtk_tree_store_append(store, &iter, NULL);
145 gtk_tree_store_set(store, &iter, 0, NULL, 1, category->name, 2, "", -1);
146 }
147 }
148
149 void streambrowser_win_set_category(streamdir_t *streamdir, category_t *category)
150 {
151 streamdir_gui_t *streamdir_gui = find_streamdir_gui_by_name(streamdir->name);
152 if (streamdir_gui == NULL) {
153 failure("gui: streambrowser_win_set_category() called with non-existent streamdir\n");
154 return;
155 }
156
157 GtkTreeView *tree_view = GTK_TREE_VIEW(streamdir_gui->tree_view);
158 GtkTreeStore *store = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(tree_view)));
159 GtkTreePath *path;
160 GtkTreeIter iter, new_iter;
161
162 /* clear all the previously added streaminfo in this category */
163 path = gtk_tree_path_new_from_indices(category_get_index(streamdir, category), 0, -1);
164 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, path)) {
165 while (gtk_tree_store_remove(store, &iter))
166 ;
167 }
168
169 /* find the corresponding category tree iter */
170 path = gtk_tree_path_new_from_indices(category_get_index(streamdir, category), -1);
171 if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, path))
172 return;
173
174 /* append the new streaminfos to the current category / iter */
175 int i, count = streaminfo_get_count(category);
176 streaminfo_t *streaminfo;
177 for (i = 0; i < count; i++) {
178 streaminfo = streaminfo_get_by_index(category, i);
179
180 gtk_tree_store_append(store, &new_iter, &iter);
181 gtk_tree_store_set(store, &new_iter, 0, NULL, 1, streaminfo->name, 2, streaminfo->current_track, -1);
182 }
183 }
184
185 void streambrowser_win_set_update_function(void (*function) (streamdir_t *streamdir, category_t *category, streaminfo_t *streaminfo))
186 {
187 update_function = function;
188 }
189
190 static GtkWidget* gtk_label_new_with_icon(gchar *icon_filename, gchar *label_text)
191 {
192 GtkWidget *hbox = gtk_hbox_new(FALSE, 1);
193 GtkWidget *label = gtk_label_new(label_text);
194 GtkWidget *icon = gtk_image_new_from_file(icon_filename);
195
196 gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, TRUE, 0);
197 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
198
199 return hbox;
200 }
201
202 static GtkWidget *gtk_streamdir_tree_view_new()
203 {
204 GtkWidget *tree_view = gtk_tree_view_new();
205
206 GtkTreeStore *store = gtk_tree_store_new(3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);
207 gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view), GTK_TREE_MODEL(store));
208
209 // todo: why doesn't the tree view allow to be resized?
210 //gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_view), FALSE);
211 gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(tree_view), TRUE);
212 //gtk_tree_view_set_reorderable(GTK_TREE_VIEW(tree_view), TRUE);
213 gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(tree_view), FALSE);
214
215 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tree_view), -1, "", cell_renderer_pixbuf, "pixbuf", 0, NULL);
216 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tree_view), -1, "Stream name", cell_renderer_text, "text", 1, NULL);
217 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tree_view), -1, "Now playing", cell_renderer_text, "text", 2, NULL);
218
219 g_signal_connect(G_OBJECT(tree_view), "cursor-changed", G_CALLBACK(on_tree_view_cursor_changed), NULL);
220
221 return tree_view;
222 }
223
224 static GtkWidget* gtk_streamdir_table_new(GtkWidget *tree_view)
225 {
226 GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
227 gtk_container_add(GTK_CONTAINER(scrolled_window), tree_view);
228
229 GtkWidget *table = gtk_table_new(1, 1, FALSE);
230 gtk_table_attach(GTK_TABLE(table), scrolled_window, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
231
232 return table;
233 }
234
235 static gboolean on_notebook_switch_page(GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer data)
236 {
237 if (page_num < 0)
238 return FALSE;
239
240 /* update the current selected stream */
241 streamdir_gui_t *streamdir_gui = g_list_nth_data(streamdir_gui_list, page_num);
242 update_function(streamdir_gui->streamdir, NULL, NULL);
243
244 return TRUE;
245 }
246
247 static gboolean on_tree_view_cursor_changed(GtkTreeView *tree_view, gpointer data)
248 {
249 GtkTreePath *path;
250 GtkTreeViewColumn *focus_column;
251
252 gtk_tree_view_get_cursor(tree_view, &path, &focus_column);
253
254 if (path == NULL)
255 return TRUE;
256
257 gint *indices = gtk_tree_path_get_indices(path);
258 if (gtk_tree_path_get_depth(path) != 1) {
259 gtk_tree_path_free(path);
260 return TRUE;
261 }
262
263 int category_index = indices[0];
264
265 gtk_tree_path_free(path);
266
267 streamdir_gui_t *streamdir_gui = find_streamdir_gui_by_tree_view(tree_view);
268 if (streamdir_gui == NULL)
269 return TRUE;
270
271 update_function(streamdir_gui->streamdir, category_get_by_index(streamdir_gui->streamdir, category_index), NULL);
272
273 return TRUE;
274 }
275
276 static gboolean on_add_button_clicked(GtkButton *button, gpointer data)
277 {
278 GtkTreePath *path;
279 GtkTreeViewColumn *focus_column;
280
281 GtkWidget *table = gtk_notebook_get_nth_page(GTK_NOTEBOOK(notebook), gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook)));
282 streamdir_gui_t *streamdir_gui = find_streamdir_gui_by_table(GTK_TABLE(table));
283 if (streamdir_gui == NULL)
284 return TRUE;
285
286 GtkWidget *tree_view = streamdir_gui->tree_view;
287
288 gtk_tree_view_get_cursor(GTK_TREE_VIEW(tree_view), &path, &focus_column);
289
290 if (path == NULL)
291 return TRUE;
292
293 gint *indices = gtk_tree_path_get_indices(path);
294 if (gtk_tree_path_get_depth(path) != 2) {
295 gtk_tree_path_free(path);
296 return TRUE;
297 }
298
299 int category_index = indices[0];
300 int streaminfo_index = indices[1];
301
302 gtk_tree_path_free(path);
303
304 streamdir_t *streamdir = streamdir_gui->streamdir;
305 category_t *category = category_get_by_index(streamdir_gui->streamdir, category_index);
306 streaminfo_t *streaminfo = streaminfo_get_by_index(category, streaminfo_index);
307
308 update_function(streamdir, category, streaminfo);
309
310 return TRUE;
311 }
312
313 static streamdir_gui_t *find_streamdir_gui_by_name(gchar *name)
314 {
315 GList *iterator;
316 streamdir_gui_t *streamdir_gui;
317
318 for (iterator = g_list_first(streamdir_gui_list); iterator != NULL; iterator = g_list_next(iterator)) {
319 streamdir_gui = iterator->data;
320
321 if (strcmp(streamdir_gui->streamdir->name, name) == 0)
322 return streamdir_gui;
323 }
324
325 return NULL;
326 }
327
328 static streamdir_gui_t *find_streamdir_gui_by_tree_view(GtkTreeView *tree_view)
329 {
330 GList *iterator;
331 streamdir_gui_t *streamdir_gui;
332
333 for (iterator = g_list_first(streamdir_gui_list); iterator != NULL; iterator = g_list_next(iterator)) {
334 streamdir_gui = iterator->data;
335
336 if ((void *) streamdir_gui->tree_view == (void *) tree_view)
337 return streamdir_gui;
338 }
339
340 return NULL;
341 }
342
343 static streamdir_gui_t *find_streamdir_gui_by_table(GtkTable *table)
344 {
345 GList *iterator;
346 streamdir_gui_t *streamdir_gui;
347
348 for (iterator = g_list_first(streamdir_gui_list); iterator != NULL; iterator = g_list_next(iterator)) {
349 streamdir_gui = iterator->data;
350
351 if ((void *) streamdir_gui->table == (void *) table)
352 return streamdir_gui;
353 }
354
355 return NULL;
356 }
357