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