Mercurial > audlegacy-plugins
annotate src/streambrowser/gui/streambrowser_win.c @ 2757:4ec0e13208de
added shoutcast icon; fixed some small bugs
| author | Calin Crisan ccrisan@gmail.com |
|---|---|
| date | Tue, 01 Jul 2008 21:13:22 +0300 |
| parents | 6d6a3eb67510 |
| children | d45b4beadf6c |
| rev | line source |
|---|---|
| 2735 | 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(); | |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
205 |
| 2735 | 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? | |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
210 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_view), TRUE); |
| 2735 | 211 gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(tree_view), TRUE); |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
212 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(tree_view), TRUE); |
| 2735 | 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); | |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
218 |
| 2735 | 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 */ | |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
241 |
|
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
242 /* |
| 2735 | 243 streamdir_gui_t *streamdir_gui = g_list_nth_data(streamdir_gui_list, page_num); |
| 244 update_function(streamdir_gui->streamdir, NULL, NULL); | |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
245 */ |
|
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
246 |
|
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
247 /* clear the search box */ |
|
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
248 gtk_entry_set_text(GTK_ENTRY(search_entry), ""); |
| 2735 | 249 |
| 250 return TRUE; | |
| 251 } | |
| 252 | |
| 253 static gboolean on_tree_view_cursor_changed(GtkTreeView *tree_view, gpointer data) | |
| 254 { | |
| 255 GtkTreePath *path; | |
| 256 GtkTreeViewColumn *focus_column; | |
| 257 | |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
258 /* obtain the current category */ |
| 2735 | 259 gtk_tree_view_get_cursor(tree_view, &path, &focus_column); |
| 260 | |
| 261 if (path == NULL) | |
| 262 return TRUE; | |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
263 |
| 2735 | 264 gint *indices = gtk_tree_path_get_indices(path); |
| 265 if (gtk_tree_path_get_depth(path) != 1) { | |
| 266 gtk_tree_path_free(path); | |
| 267 return TRUE; | |
| 268 } | |
| 269 | |
| 270 int category_index = indices[0]; | |
| 271 | |
| 272 gtk_tree_path_free(path); | |
| 273 | |
| 274 streamdir_gui_t *streamdir_gui = find_streamdir_gui_by_tree_view(tree_view); | |
| 275 if (streamdir_gui == NULL) | |
| 276 return TRUE; | |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
277 |
|
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
278 /* issue an update on the current category */ |
| 2735 | 279 update_function(streamdir_gui->streamdir, category_get_by_index(streamdir_gui->streamdir, category_index), NULL); |
|
2757
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
280 |
|
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
281 /* clear the search box */ |
|
4ec0e13208de
added shoutcast icon; fixed some small bugs
Calin Crisan ccrisan@gmail.com
parents:
2735
diff
changeset
|
282 gtk_entry_set_text(GTK_ENTRY(search_entry), ""); |
| 2735 | 283 |
| 284 return TRUE; | |
| 285 } | |
| 286 | |
| 287 static gboolean on_add_button_clicked(GtkButton *button, gpointer data) | |
| 288 { | |
| 289 GtkTreePath *path; | |
| 290 GtkTreeViewColumn *focus_column; | |
| 291 | |
| 292 GtkWidget *table = gtk_notebook_get_nth_page(GTK_NOTEBOOK(notebook), gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))); | |
| 293 streamdir_gui_t *streamdir_gui = find_streamdir_gui_by_table(GTK_TABLE(table)); | |
| 294 if (streamdir_gui == NULL) | |
| 295 return TRUE; | |
| 296 | |
| 297 GtkWidget *tree_view = streamdir_gui->tree_view; | |
| 298 | |
| 299 gtk_tree_view_get_cursor(GTK_TREE_VIEW(tree_view), &path, &focus_column); | |
| 300 | |
| 301 if (path == NULL) | |
| 302 return TRUE; | |
| 303 | |
| 304 gint *indices = gtk_tree_path_get_indices(path); | |
| 305 if (gtk_tree_path_get_depth(path) != 2) { | |
| 306 gtk_tree_path_free(path); | |
| 307 return TRUE; | |
| 308 } | |
| 309 | |
| 310 int category_index = indices[0]; | |
| 311 int streaminfo_index = indices[1]; | |
| 312 | |
| 313 gtk_tree_path_free(path); | |
| 314 | |
| 315 streamdir_t *streamdir = streamdir_gui->streamdir; | |
| 316 category_t *category = category_get_by_index(streamdir_gui->streamdir, category_index); | |
| 317 streaminfo_t *streaminfo = streaminfo_get_by_index(category, streaminfo_index); | |
| 318 | |
| 319 update_function(streamdir, category, streaminfo); | |
| 320 | |
| 321 return TRUE; | |
| 322 } | |
| 323 | |
| 324 static streamdir_gui_t *find_streamdir_gui_by_name(gchar *name) | |
| 325 { | |
| 326 GList *iterator; | |
| 327 streamdir_gui_t *streamdir_gui; | |
| 328 | |
| 329 for (iterator = g_list_first(streamdir_gui_list); iterator != NULL; iterator = g_list_next(iterator)) { | |
| 330 streamdir_gui = iterator->data; | |
| 331 | |
| 332 if (strcmp(streamdir_gui->streamdir->name, name) == 0) | |
| 333 return streamdir_gui; | |
| 334 } | |
| 335 | |
| 336 return NULL; | |
| 337 } | |
| 338 | |
| 339 static streamdir_gui_t *find_streamdir_gui_by_tree_view(GtkTreeView *tree_view) | |
| 340 { | |
| 341 GList *iterator; | |
| 342 streamdir_gui_t *streamdir_gui; | |
| 343 | |
| 344 for (iterator = g_list_first(streamdir_gui_list); iterator != NULL; iterator = g_list_next(iterator)) { | |
| 345 streamdir_gui = iterator->data; | |
| 346 | |
| 347 if ((void *) streamdir_gui->tree_view == (void *) tree_view) | |
| 348 return streamdir_gui; | |
| 349 } | |
| 350 | |
| 351 return NULL; | |
| 352 } | |
| 353 | |
| 354 static streamdir_gui_t *find_streamdir_gui_by_table(GtkTable *table) | |
| 355 { | |
| 356 GList *iterator; | |
| 357 streamdir_gui_t *streamdir_gui; | |
| 358 | |
| 359 for (iterator = g_list_first(streamdir_gui_list); iterator != NULL; iterator = g_list_next(iterator)) { | |
| 360 streamdir_gui = iterator->data; | |
| 361 | |
| 362 if ((void *) streamdir_gui->table == (void *) table) | |
| 363 return streamdir_gui; | |
| 364 } | |
| 365 | |
| 366 return NULL; | |
| 367 } | |
| 368 |
