X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/487f69b2928dad34a2167fe24e8fc4788345f48c..61366360ff395427a76472e407debad7d3cbecdf:/disobedience/choose-menu.c diff --git a/disobedience/choose-menu.c b/disobedience/choose-menu.c index 460acc7..37167e7 100644 --- a/disobedience/choose-menu.c +++ b/disobedience/choose-menu.c @@ -2,96 +2,183 @@ * This file is part of DisOrder * Copyright (C) 2008 Richard Kettlewell * - * This program is free software; you can redistribute it and/or modify + * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * along with this program. If not, see . + */ +/** @file disobedience/choose-menu.c + * @brief Popup menu for choose screen */ #include "disobedience.h" #include "popup.h" #include "choose.h" -VECTOR_TYPE(cdvector, struct choosedata *, xrealloc); - /** @brief Popup menu */ static GtkWidget *choose_menu; -/** @brief Callback for choose_get_selected() */ -static void choose_gather_selected_callback(GtkTreeModel attribute((unused)) *model, - GtkTreePath attribute((unused)) *path, - GtkTreeIter *iter, - gpointer data) { - struct cdvector *v = data; - struct choosedata *cd = choose_iter_to_data(iter); - - if(cd) - cdvector_append(v, cd); +/** @brief Recursion step for choose_get_visible() + * @param parent A visible node, or NULL for the root + * @param callback Called for each visible node + * @param userdata Passed to @p callback + * + * If @p callback returns nonzero, the walk stops immediately. + */ +static int choose_visible_recurse(GtkTreeIter *parent, + int (*callback)(GtkTreeIter *it, + int isfile, + void *userdata), + void *userdata) { + int expanded; + if(parent) { + /* Skip placeholders */ + if(choose_is_placeholder(parent)) + return 0; + const int isfile = choose_is_file(parent); + if(callback(parent, isfile, userdata)) + return 1; + if(isfile) + return 0; /* Files never have children */ + GtkTreePath *parent_path + = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store), + parent); + expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view), + parent_path); + gtk_tree_path_free(parent_path); + } else + expanded = 1; + /* See if parent is expanded */ + if(expanded) { + /* Parent is expanded, visit all its children */ + GtkTreeIter it[1]; + gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), + it, + parent); + while(itv) { + if(choose_visible_recurse(it, callback, userdata)) + return TRUE; + itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it); + } + } + return 0; } -/** @brief Get a list of all selected tracks and directories */ -static struct choosedata **choose_get_selected(int *nselected) { - struct cdvector v[1]; +static void choose_visible_visit(int (*callback)(GtkTreeIter *it, + int isfile, + void *userdata), + void *userdata) { + choose_visible_recurse(NULL, callback, userdata); +} - cdvector_init(v); - gtk_tree_selection_selected_foreach(choose_selection, - choose_gather_selected_callback, - v); - cdvector_terminate(v); - if(nselected) - *nselected = v->nvec; - return v->vec; +static int choose_selectall_sensitive_callback + (GtkTreeIter attribute((unused)) *it, + int isfile, + void *userdata) { + if(isfile) { + *(int *)userdata = 1; + return 1; + } + return 0; } +/** @brief Should 'select all' be sensitive? + * + * Yes if there are visible files. + */ static int choose_selectall_sensitive(void attribute((unused)) *extra) { - return TRUE; + int files = 0; + choose_visible_visit(choose_selectall_sensitive_callback, &files); + return files > 0; } - + +static int choose_selectall_activate_callback + (GtkTreeIter *it, + int isfile, + void attribute((unused)) *userdata) { + if(isfile) + gtk_tree_selection_select_iter(choose_selection, it); + else + gtk_tree_selection_unselect_iter(choose_selection, it); + return 0; +} + +/** @brief Activate select all + * + * Selects all files and deselects everything else. + */ static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item, gpointer attribute((unused)) userdata) { - gtk_tree_selection_select_all(choose_selection); + choose_visible_visit(choose_selectall_activate_callback, 0); } - + +/** @brief Should 'select none' be sensitive + * + * Yes if anything is selected. + */ static int choose_selectnone_sensitive(void attribute((unused)) *extra) { return gtk_tree_selection_count_selected_rows(choose_selection) > 0; } - + +/** @brief Activate select none */ static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item, gpointer attribute((unused)) userdata) { gtk_tree_selection_unselect_all(choose_selection); } - + +static void choose_play_sensitive_callback(GtkTreeModel attribute((unused)) *model, + GtkTreePath attribute((unused)) *path, + GtkTreeIter *iter, + gpointer data) { + int *filesp = data; + + if(*filesp == -1) + return; + if(choose_is_dir(iter)) + *filesp = -1; + else if(choose_is_file(iter)) + ++*filesp; +} + +/** @brief Should 'play' be sensitive? + * + * Yes if tracks are selected and no directories are */ static int choose_play_sensitive(void attribute((unused)) *extra) { - struct choosedata *cd, **cdp = choose_get_selected(NULL); - int counts[2] = { 0, 0 }; - while((cd = *cdp++)) - ++counts[cd->type]; - return !counts[CHOOSE_DIRECTORY] && counts[CHOOSE_FILE]; + int files = 0; + + gtk_tree_selection_selected_foreach(choose_selection, + choose_play_sensitive_callback, + &files); + return files > 0; } -static void choose_play_completed(void attribute((unused)) *v, - const char *error) { - if(error) - popup_protocol_error(0, error); +static void choose_gather_selected_files_callback(GtkTreeModel attribute((unused)) *model, + GtkTreePath attribute((unused)) *path, + GtkTreeIter *iter, + gpointer data) { + struct vector *v = data; + + if(choose_is_file(iter)) + vector_append(v, choose_get_track(iter)); } + static void choose_play_activate(GtkMenuItem attribute((unused)) *item, gpointer attribute((unused)) userdata) { - struct choosedata *cd, **cdp = choose_get_selected(NULL); - while((cd = *cdp++)) { - if(cd->type == CHOOSE_FILE) - disorder_eclient_play(client, xstrdup(cd->track), - choose_play_completed, 0); - } + struct vector v[1]; + vector_init(v); + gtk_tree_selection_selected_foreach(choose_selection, + choose_gather_selected_files_callback, + v); + for(int n = 0; n < v->nvec; ++n) + disorder_eclient_play(client, v->vec[n], choose_play_completed, 0); } static int choose_properties_sensitive(void *extra) { @@ -100,11 +187,11 @@ static int choose_properties_sensitive(void *extra) { static void choose_properties_activate(GtkMenuItem attribute((unused)) *item, gpointer attribute((unused)) userdata) { - struct choosedata *cd, **cdp = choose_get_selected(NULL); struct vector v[1]; vector_init(v); - while((cd = *cdp++)) - vector_append(v, xstrdup(cd->track)); + gtk_tree_selection_selected_foreach(choose_selection, + choose_gather_selected_files_callback, + v); properties(v->nvec, (const char **)v->vec); }