2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 #include "disobedience.h"
24 VECTOR_TYPE(cdvector, struct choosedata *, xrealloc);
26 /** @brief Popup menu */
27 static GtkWidget *choose_menu;
29 /** @brief Callback for choose_get_selected() */
30 static void choose_gather_selected_callback(GtkTreeModel attribute((unused)) *model,
31 GtkTreePath attribute((unused)) *path,
34 struct cdvector *v = data;
35 struct choosedata *cd = choose_iter_to_data(iter);
38 cdvector_append(v, cd);
41 /** @brief Get a list of all selected tracks and directories */
42 static struct choosedata **choose_get_selected(int *nselected) {
46 gtk_tree_selection_selected_foreach(choose_selection,
47 choose_gather_selected_callback,
49 cdvector_terminate(v);
55 /** @brief Recursion step for choose_get_visible()
56 * @param parent A visible node, or NULL for the root
57 * @param cdv Visible nodes accumulated here
59 static void choose_visible_recurse(GtkTreeIter *parent,
60 void (*callback)(GtkTreeIter *it,
61 struct choosedata *cd,
64 struct choosedata *cd;
67 cd = choose_iter_to_data(parent);
68 callback(parent, cd, userdata);
69 if(cd->type != CHOOSE_DIRECTORY)
70 /* Only directories can be expanded so we can avoid the more
71 * expensive test below */
73 GtkTreePath *parent_path
74 = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store),
76 expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view),
78 gtk_tree_path_free(parent_path);
81 /* See if parent is expanded */
83 /* Parent is expanded, visit all its children */
85 gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
89 choose_visible_recurse(it, callback, userdata);
90 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
95 static void choose_visible_visit(void (*callback)(GtkTreeIter *it,
96 struct choosedata *cd,
99 choose_visible_recurse(NULL, callback, userdata);
102 static void count_choosedatas(struct choosedata **cds,
104 struct choosedata *cd;
105 counts[CHOOSE_FILE] = counts[CHOOSE_DIRECTORY] = 0;
111 static void choose_selectall_sensitive_callback
112 (GtkTreeIter attribute((unused)) *it,
113 struct choosedata *cd,
115 if(cd->type == CHOOSE_FILE)
119 static int choose_selectall_sensitive(void attribute((unused)) *extra) {
121 choose_visible_visit(choose_selectall_sensitive_callback, &files);
125 static void choose_selectall_activate_callback
127 struct choosedata *cd,
128 void attribute((unused)) *userdata) {
129 if(cd->type == CHOOSE_FILE)
130 gtk_tree_selection_select_iter(choose_selection, it);
132 gtk_tree_selection_unselect_iter(choose_selection, it);
135 static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item,
136 gpointer attribute((unused)) userdata) {
137 choose_visible_visit(choose_selectall_activate_callback, 0);
140 static int choose_selectnone_sensitive(void attribute((unused)) *extra) {
141 return gtk_tree_selection_count_selected_rows(choose_selection) > 0;
144 static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item,
145 gpointer attribute((unused)) userdata) {
146 gtk_tree_selection_unselect_all(choose_selection);
149 static int choose_play_sensitive(void attribute((unused)) *extra) {
151 count_choosedatas(choose_get_selected(NULL), counts);
152 return !counts[CHOOSE_DIRECTORY] && counts[CHOOSE_FILE];
155 static void choose_play_completed(void attribute((unused)) *v,
158 popup_protocol_error(0, error);
161 static void choose_play_activate(GtkMenuItem attribute((unused)) *item,
162 gpointer attribute((unused)) userdata) {
163 struct choosedata *cd, **cdp = choose_get_selected(NULL);
164 while((cd = *cdp++)) {
165 if(cd->type == CHOOSE_FILE)
166 disorder_eclient_play(client, xstrdup(cd->track),
167 choose_play_completed, 0);
171 static int choose_properties_sensitive(void *extra) {
172 return choose_play_sensitive(extra);
175 static void choose_properties_activate(GtkMenuItem attribute((unused)) *item,
176 gpointer attribute((unused)) userdata) {
177 struct choosedata *cd, **cdp = choose_get_selected(NULL);
181 vector_append(v, xstrdup(cd->track));
182 properties(v->nvec, (const char **)v->vec);
185 /** @brief Pop-up menu for choose */
186 static struct menuitem choose_menuitems[] = {
189 choose_play_activate,
190 choose_play_sensitive,
196 choose_properties_activate,
197 choose_properties_sensitive,
203 choose_selectall_activate,
204 choose_selectall_sensitive,
209 "Deselect all tracks",
210 choose_selectnone_activate,
211 choose_selectnone_sensitive,
217 const struct tabtype choose_tabtype = {
218 choose_properties_sensitive,
219 choose_selectall_sensitive,
220 choose_selectnone_sensitive,
221 choose_properties_activate,
222 choose_selectall_activate,
223 choose_selectnone_activate,
228 /** @brief Called when a mouse button is pressed or released */
229 gboolean choose_button_event(GtkWidget attribute((unused)) *widget,
230 GdkEventButton *event,
231 gpointer attribute((unused)) user_data) {
232 if(event->type == GDK_BUTTON_RELEASE && event->button == 2) {
233 /* Middle click release - play track */
234 ensure_selected(GTK_TREE_VIEW(choose_view), event);
235 choose_play_activate(NULL, NULL);
236 } else if(event->type == GDK_BUTTON_PRESS && event->button == 3) {
237 /* Right click press - pop up the menu */
238 ensure_selected(GTK_TREE_VIEW(choose_view), event);
239 popup(&choose_menu, event,
240 choose_menuitems, sizeof choose_menuitems / sizeof *choose_menuitems,