| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2008 Richard Kettlewell |
| 4 | * |
| 5 | * This program 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 | * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "disobedience.h" |
| 19 | #include "popup.h" |
| 20 | #include "choose.h" |
| 21 | |
| 22 | /** @brief Popup menu */ |
| 23 | static GtkWidget *choose_menu; |
| 24 | |
| 25 | /** @brief Recursion step for choose_get_visible() |
| 26 | * @param parent A visible node, or NULL for the root |
| 27 | * @param callback Called for each visible node |
| 28 | * @param userdata Passed to @p callback |
| 29 | * |
| 30 | * If @p callback returns nonzero, the walk stops immediately. |
| 31 | */ |
| 32 | static int choose_visible_recurse(GtkTreeIter *parent, |
| 33 | int (*callback)(GtkTreeIter *it, |
| 34 | int isfile, |
| 35 | void *userdata), |
| 36 | void *userdata) { |
| 37 | int expanded; |
| 38 | if(parent) { |
| 39 | /* Skip placeholders */ |
| 40 | if(choose_is_placeholder(parent)) |
| 41 | return 0; |
| 42 | const int isfile = choose_is_file(parent); |
| 43 | if(callback(parent, isfile, userdata)) |
| 44 | return 1; |
| 45 | if(isfile) |
| 46 | return 0; /* Files never have children */ |
| 47 | GtkTreePath *parent_path |
| 48 | = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store), |
| 49 | parent); |
| 50 | expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view), |
| 51 | parent_path); |
| 52 | gtk_tree_path_free(parent_path); |
| 53 | } else |
| 54 | expanded = 1; |
| 55 | /* See if parent is expanded */ |
| 56 | if(expanded) { |
| 57 | /* Parent is expanded, visit all its children */ |
| 58 | GtkTreeIter it[1]; |
| 59 | gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), |
| 60 | it, |
| 61 | parent); |
| 62 | while(itv) { |
| 63 | if(choose_visible_recurse(it, callback, userdata)) |
| 64 | return TRUE; |
| 65 | itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it); |
| 66 | } |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static void choose_visible_visit(int (*callback)(GtkTreeIter *it, |
| 72 | int isfile, |
| 73 | void *userdata), |
| 74 | void *userdata) { |
| 75 | choose_visible_recurse(NULL, callback, userdata); |
| 76 | } |
| 77 | |
| 78 | static int choose_selectall_sensitive_callback |
| 79 | (GtkTreeIter attribute((unused)) *it, |
| 80 | int isfile, |
| 81 | void *userdata) { |
| 82 | if(isfile) { |
| 83 | *(int *)userdata = 1; |
| 84 | return 1; |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /** @brief Should 'select all' be sensitive? |
| 90 | * |
| 91 | * Yes if there are visible files. |
| 92 | */ |
| 93 | static int choose_selectall_sensitive(void attribute((unused)) *extra) { |
| 94 | int files = 0; |
| 95 | choose_visible_visit(choose_selectall_sensitive_callback, &files); |
| 96 | return files > 0; |
| 97 | } |
| 98 | |
| 99 | static int choose_selectall_activate_callback |
| 100 | (GtkTreeIter *it, |
| 101 | int isfile, |
| 102 | void attribute((unused)) *userdata) { |
| 103 | if(isfile) |
| 104 | gtk_tree_selection_select_iter(choose_selection, it); |
| 105 | else |
| 106 | gtk_tree_selection_unselect_iter(choose_selection, it); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /** @brief Activate select all |
| 111 | * |
| 112 | * Selects all files and deselects everything else. |
| 113 | */ |
| 114 | static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item, |
| 115 | gpointer attribute((unused)) userdata) { |
| 116 | choose_visible_visit(choose_selectall_activate_callback, 0); |
| 117 | } |
| 118 | |
| 119 | /** @brief Should 'select none' be sensitive |
| 120 | * |
| 121 | * Yes if anything is selected. |
| 122 | */ |
| 123 | static int choose_selectnone_sensitive(void attribute((unused)) *extra) { |
| 124 | return gtk_tree_selection_count_selected_rows(choose_selection) > 0; |
| 125 | } |
| 126 | |
| 127 | /** @brief Activate select none */ |
| 128 | static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item, |
| 129 | gpointer attribute((unused)) userdata) { |
| 130 | gtk_tree_selection_unselect_all(choose_selection); |
| 131 | } |
| 132 | |
| 133 | static void choose_play_sensitive_callback(GtkTreeModel attribute((unused)) *model, |
| 134 | GtkTreePath attribute((unused)) *path, |
| 135 | GtkTreeIter *iter, |
| 136 | gpointer data) { |
| 137 | int *filesp = data; |
| 138 | |
| 139 | if(*filesp == -1) |
| 140 | return; |
| 141 | if(choose_is_dir(iter)) |
| 142 | *filesp = -1; |
| 143 | else if(choose_is_file(iter)) |
| 144 | ++*filesp; |
| 145 | } |
| 146 | |
| 147 | /** @brief Should 'play' be sensitive? |
| 148 | * |
| 149 | * Yes if tracks are selected and no directories are */ |
| 150 | static int choose_play_sensitive(void attribute((unused)) *extra) { |
| 151 | int files = 0; |
| 152 | |
| 153 | gtk_tree_selection_selected_foreach(choose_selection, |
| 154 | choose_play_sensitive_callback, |
| 155 | &files); |
| 156 | return files > 0; |
| 157 | } |
| 158 | |
| 159 | static void choose_gather_selected_files_callback(GtkTreeModel attribute((unused)) *model, |
| 160 | GtkTreePath attribute((unused)) *path, |
| 161 | GtkTreeIter *iter, |
| 162 | gpointer data) { |
| 163 | struct vector *v = data; |
| 164 | |
| 165 | if(choose_is_file(iter)) |
| 166 | vector_append(v, choose_get_track(iter)); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | static void choose_play_activate(GtkMenuItem attribute((unused)) *item, |
| 171 | gpointer attribute((unused)) userdata) { |
| 172 | struct vector v[1]; |
| 173 | vector_init(v); |
| 174 | gtk_tree_selection_selected_foreach(choose_selection, |
| 175 | choose_gather_selected_files_callback, |
| 176 | v); |
| 177 | for(int n = 0; n < v->nvec; ++n) |
| 178 | disorder_eclient_play(client, v->vec[n], choose_play_completed, 0); |
| 179 | } |
| 180 | |
| 181 | static int choose_properties_sensitive(void *extra) { |
| 182 | return choose_play_sensitive(extra); |
| 183 | } |
| 184 | |
| 185 | static void choose_properties_activate(GtkMenuItem attribute((unused)) *item, |
| 186 | gpointer attribute((unused)) userdata) { |
| 187 | struct vector v[1]; |
| 188 | vector_init(v); |
| 189 | gtk_tree_selection_selected_foreach(choose_selection, |
| 190 | choose_gather_selected_files_callback, |
| 191 | v); |
| 192 | properties(v->nvec, (const char **)v->vec); |
| 193 | } |
| 194 | |
| 195 | /** @brief Pop-up menu for choose */ |
| 196 | static struct menuitem choose_menuitems[] = { |
| 197 | { |
| 198 | "Play track", |
| 199 | choose_play_activate, |
| 200 | choose_play_sensitive, |
| 201 | 0, |
| 202 | 0 |
| 203 | }, |
| 204 | { |
| 205 | "Track properties", |
| 206 | choose_properties_activate, |
| 207 | choose_properties_sensitive, |
| 208 | 0, |
| 209 | 0 |
| 210 | }, |
| 211 | { |
| 212 | "Select all tracks", |
| 213 | choose_selectall_activate, |
| 214 | choose_selectall_sensitive, |
| 215 | 0, |
| 216 | 0 |
| 217 | }, |
| 218 | { |
| 219 | "Deselect all tracks", |
| 220 | choose_selectnone_activate, |
| 221 | choose_selectnone_sensitive, |
| 222 | 0, |
| 223 | 0 |
| 224 | }, |
| 225 | }; |
| 226 | |
| 227 | const struct tabtype choose_tabtype = { |
| 228 | choose_properties_sensitive, |
| 229 | choose_selectall_sensitive, |
| 230 | choose_selectnone_sensitive, |
| 231 | choose_properties_activate, |
| 232 | choose_selectall_activate, |
| 233 | choose_selectnone_activate, |
| 234 | 0, |
| 235 | 0 |
| 236 | }; |
| 237 | |
| 238 | /** @brief Called when a mouse button is pressed or released */ |
| 239 | gboolean choose_button_event(GtkWidget attribute((unused)) *widget, |
| 240 | GdkEventButton *event, |
| 241 | gpointer attribute((unused)) user_data) { |
| 242 | if(event->type == GDK_BUTTON_RELEASE && event->button == 2) { |
| 243 | /* Middle click release - play track */ |
| 244 | ensure_selected(GTK_TREE_VIEW(choose_view), event); |
| 245 | choose_play_activate(NULL, NULL); |
| 246 | } else if(event->type == GDK_BUTTON_PRESS && event->button == 3) { |
| 247 | /* Right click press - pop up the menu */ |
| 248 | ensure_selected(GTK_TREE_VIEW(choose_view), event); |
| 249 | popup(&choose_menu, event, |
| 250 | choose_menuitems, sizeof choose_menuitems / sizeof *choose_menuitems, |
| 251 | 0); |
| 252 | return TRUE; |
| 253 | } |
| 254 | return FALSE; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | Local Variables: |
| 259 | c-basic-offset:2 |
| 260 | comment-column:40 |
| 261 | fill-column:79 |
| 262 | indent-tabs-mode:nil |
| 263 | End: |
| 264 | */ |