chiark / gitweb /
Playing checkbox in Disobedience choose tab is now only visible for
[disorder] / disobedience / choose-menu.c
CommitLineData
6982880f
RK
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 2 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, 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.
14 *
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
18 * USA
19 */
20#include "disobedience.h"
21#include "popup.h"
22#include "choose.h"
23
487f69b2
RK
24VECTOR_TYPE(cdvector, struct choosedata *, xrealloc);
25
6982880f
RK
26/** @brief Popup menu */
27static GtkWidget *choose_menu;
28
487f69b2
RK
29/** @brief Callback for choose_get_selected() */
30static void choose_gather_selected_callback(GtkTreeModel attribute((unused)) *model,
31 GtkTreePath attribute((unused)) *path,
32 GtkTreeIter *iter,
33 gpointer data) {
34 struct cdvector *v = data;
35 struct choosedata *cd = choose_iter_to_data(iter);
36
37 if(cd)
38 cdvector_append(v, cd);
39}
40
41/** @brief Get a list of all selected tracks and directories */
42static struct choosedata **choose_get_selected(int *nselected) {
43 struct cdvector v[1];
44
45 cdvector_init(v);
46 gtk_tree_selection_selected_foreach(choose_selection,
47 choose_gather_selected_callback,
48 v);
49 cdvector_terminate(v);
50 if(nselected)
51 *nselected = v->nvec;
52 return v->vec;
53}
54
e8a26cdd
RK
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
58 */
59static void choose_visible_recurse(GtkTreeIter *parent,
60 void (*callback)(GtkTreeIter *it,
61 struct choosedata *cd,
62 void *userdata),
63 void *userdata) {
64 struct choosedata *cd;
65 int expanded;
66 if(parent) {
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 */
72 return;
73 GtkTreePath *parent_path
74 = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store),
75 parent);
76 expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view),
77 parent_path);
78 gtk_tree_path_free(parent_path);
79 } else
80 expanded = 1;
81 /* See if parent is expanded */
82 if(expanded) {
83 /* Parent is expanded, visit all its children */
84 GtkTreeIter it[1];
85 gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
86 it,
87 parent);
88 while(itv) {
89 choose_visible_recurse(it, callback, userdata);
90 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
91 }
92 }
93}
94
95static void choose_visible_visit(void (*callback)(GtkTreeIter *it,
96 struct choosedata *cd,
97 void *userdata),
98 void *userdata) {
99 choose_visible_recurse(NULL, callback, userdata);
100}
101
102static void count_choosedatas(struct choosedata **cds,
103 int counts[2]) {
104 struct choosedata *cd;
105 counts[CHOOSE_FILE] = counts[CHOOSE_DIRECTORY] = 0;
106 while((cd = *cds++))
107 ++counts[cd->type];
108}
109
110
111static void choose_selectall_sensitive_callback
112 (GtkTreeIter attribute((unused)) *it,
113 struct choosedata *cd,
114 void *userdata) {
115 if(cd->type == CHOOSE_FILE)
116 ++*(int *)userdata;
117}
118
6982880f 119static int choose_selectall_sensitive(void attribute((unused)) *extra) {
e8a26cdd
RK
120 int files = 0;
121 choose_visible_visit(choose_selectall_sensitive_callback, &files);
122 return files > 0;
6982880f 123}
e8a26cdd
RK
124
125static void choose_selectall_activate_callback
126 (GtkTreeIter *it,
127 struct choosedata *cd,
128 void attribute((unused)) *userdata) {
129 if(cd->type == CHOOSE_FILE)
130 gtk_tree_selection_select_iter(choose_selection, it);
131 else
132 gtk_tree_selection_unselect_iter(choose_selection, it);
133}
134
6982880f
RK
135static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item,
136 gpointer attribute((unused)) userdata) {
e8a26cdd 137 choose_visible_visit(choose_selectall_activate_callback, 0);
6982880f
RK
138}
139
140static int choose_selectnone_sensitive(void attribute((unused)) *extra) {
141 return gtk_tree_selection_count_selected_rows(choose_selection) > 0;
142}
143
144static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item,
145 gpointer attribute((unused)) userdata) {
146 gtk_tree_selection_unselect_all(choose_selection);
147}
148
149static int choose_play_sensitive(void attribute((unused)) *extra) {
e8a26cdd
RK
150 int counts[2];
151 count_choosedatas(choose_get_selected(NULL), counts);
487f69b2
RK
152 return !counts[CHOOSE_DIRECTORY] && counts[CHOOSE_FILE];
153}
6982880f
RK
154
155static void choose_play_activate(GtkMenuItem attribute((unused)) *item,
156 gpointer attribute((unused)) userdata) {
487f69b2
RK
157 struct choosedata *cd, **cdp = choose_get_selected(NULL);
158 while((cd = *cdp++)) {
159 if(cd->type == CHOOSE_FILE)
160 disorder_eclient_play(client, xstrdup(cd->track),
161 choose_play_completed, 0);
162 }
6982880f
RK
163}
164
487f69b2
RK
165static int choose_properties_sensitive(void *extra) {
166 return choose_play_sensitive(extra);
6982880f
RK
167}
168
169static void choose_properties_activate(GtkMenuItem attribute((unused)) *item,
170 gpointer attribute((unused)) userdata) {
487f69b2
RK
171 struct choosedata *cd, **cdp = choose_get_selected(NULL);
172 struct vector v[1];
173 vector_init(v);
174 while((cd = *cdp++))
175 vector_append(v, xstrdup(cd->track));
176 properties(v->nvec, (const char **)v->vec);
6982880f
RK
177}
178
179/** @brief Pop-up menu for choose */
180static struct menuitem choose_menuitems[] = {
181 {
182 "Play track",
183 choose_play_activate,
184 choose_play_sensitive,
185 0,
186 0
187 },
188 {
189 "Track properties",
190 choose_properties_activate,
191 choose_properties_sensitive,
192 0,
193 0
194 },
195 {
196 "Select all tracks",
197 choose_selectall_activate,
198 choose_selectall_sensitive,
199 0,
200 0
201 },
202 {
203 "Deselect all tracks",
204 choose_selectnone_activate,
205 choose_selectnone_sensitive,
206 0,
207 0
208 },
209};
210
211const struct tabtype choose_tabtype = {
212 choose_properties_sensitive,
213 choose_selectall_sensitive,
214 choose_selectnone_sensitive,
215 choose_properties_activate,
216 choose_selectall_activate,
217 choose_selectnone_activate,
218 0,
219 0
220};
221
222/** @brief Called when a mouse button is pressed or released */
223gboolean choose_button_event(GtkWidget attribute((unused)) *widget,
224 GdkEventButton *event,
225 gpointer attribute((unused)) user_data) {
226 if(event->type == GDK_BUTTON_RELEASE && event->button == 2) {
227 /* Middle click release - play track */
487f69b2
RK
228 ensure_selected(GTK_TREE_VIEW(choose_view), event);
229 choose_play_activate(NULL, NULL);
6982880f
RK
230 } else if(event->type == GDK_BUTTON_PRESS && event->button == 3) {
231 /* Right click press - pop up the menu */
232 ensure_selected(GTK_TREE_VIEW(choose_view), event);
233 popup(&choose_menu, event,
234 choose_menuitems, sizeof choose_menuitems / sizeof *choose_menuitems,
235 0);
236 return TRUE;
237 }
238 return FALSE;
239}
240
241/*
242Local Variables:
243c-basic-offset:2
244comment-column:40
245fill-column:79
246indent-tabs-mode:nil
247End:
248*/