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 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
18 /** @file disobedience/choose-menu.c
19 * @brief Popup menu for choose screen
21 #include "disobedience.h"
25 /** @brief Popup menu */
26 static GtkWidget
*choose_menu
;
28 /** @brief Recursion step for choose_get_visible()
29 * @param parent A visible node, or NULL for the root
30 * @param callback Called for each visible node
31 * @param userdata Passed to @p callback
33 * If @p callback returns nonzero, the walk stops immediately.
35 static int choose_visible_recurse(GtkTreeIter
*parent
,
36 int (*callback
)(GtkTreeIter
*it
,
42 /* Skip placeholders */
43 if(choose_is_placeholder(parent
))
45 const int isfile
= choose_is_file(parent
);
46 if(callback(parent
, isfile
, userdata
))
49 return 0; /* Files never have children */
50 GtkTreePath
*parent_path
51 = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store
),
53 expanded
= gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view
),
55 gtk_tree_path_free(parent_path
);
58 /* See if parent is expanded */
60 /* Parent is expanded, visit all its children */
62 gboolean itv
= gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store
),
66 if(choose_visible_recurse(it
, callback
, userdata
))
68 itv
= gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store
), it
);
74 static void choose_visible_visit(int (*callback
)(GtkTreeIter
*it
,
78 choose_visible_recurse(NULL
, callback
, userdata
);
81 static int choose_selectall_sensitive_callback
82 (GtkTreeIter
attribute((unused
)) *it
,
92 /** @brief Should 'select all' be sensitive?
94 * Yes if there are visible files.
96 static int choose_selectall_sensitive(void attribute((unused
)) *extra
) {
98 choose_visible_visit(choose_selectall_sensitive_callback
, &files
);
102 static int choose_selectall_activate_callback
105 void attribute((unused
)) *userdata
) {
107 gtk_tree_selection_select_iter(choose_selection
, it
);
109 gtk_tree_selection_unselect_iter(choose_selection
, it
);
113 /** @brief Activate select all
115 * Selects all files and deselects everything else.
117 static void choose_selectall_activate(GtkMenuItem
attribute((unused
)) *item
,
118 gpointer
attribute((unused
)) userdata
) {
119 choose_visible_visit(choose_selectall_activate_callback
, 0);
122 /** @brief Should 'select none' be sensitive
124 * Yes if anything is selected.
126 static int choose_selectnone_sensitive(void attribute((unused
)) *extra
) {
127 return gtk_tree_selection_count_selected_rows(choose_selection
) > 0;
130 /** @brief Activate select none */
131 static void choose_selectnone_activate(GtkMenuItem
attribute((unused
)) *item
,
132 gpointer
attribute((unused
)) userdata
) {
133 gtk_tree_selection_unselect_all(choose_selection
);
136 static void choose_play_sensitive_callback(GtkTreeModel
attribute((unused
)) *model
,
137 GtkTreePath
attribute((unused
)) *path
,
144 if(choose_is_dir(iter
))
146 else if(choose_is_file(iter
))
150 /** @brief Should 'play' be sensitive?
152 * Yes if tracks are selected and no directories are */
153 static int choose_play_sensitive(void attribute((unused
)) *extra
) {
156 gtk_tree_selection_selected_foreach(choose_selection
,
157 choose_play_sensitive_callback
,
162 static void choose_gather_selected_files_callback(GtkTreeModel
attribute((unused
)) *model
,
163 GtkTreePath
attribute((unused
)) *path
,
166 struct vector
*v
= data
;
168 if(choose_is_file(iter
))
169 vector_append(v
, choose_get_track(iter
));
173 static void choose_play_activate(GtkMenuItem
attribute((unused
)) *item
,
174 gpointer
attribute((unused
)) userdata
) {
177 gtk_tree_selection_selected_foreach(choose_selection
,
178 choose_gather_selected_files_callback
,
180 for(int n
= 0; n
< v
->nvec
; ++n
)
181 disorder_eclient_play(client
, v
->vec
[n
], choose_play_completed
, 0);
184 static int choose_properties_sensitive(void *extra
) {
185 return choose_play_sensitive(extra
);
188 static void choose_properties_activate(GtkMenuItem
attribute((unused
)) *item
,
189 gpointer
attribute((unused
)) userdata
) {
192 gtk_tree_selection_selected_foreach(choose_selection
,
193 choose_gather_selected_files_callback
,
195 properties(v
->nvec
, (const char **)v
->vec
);
198 /** @brief Pop-up menu for choose */
199 static struct menuitem choose_menuitems
[] = {
202 choose_play_activate
,
203 choose_play_sensitive
,
209 choose_properties_activate
,
210 choose_properties_sensitive
,
216 choose_selectall_activate
,
217 choose_selectall_sensitive
,
222 "Deselect all tracks",
223 choose_selectnone_activate
,
224 choose_selectnone_sensitive
,
230 const struct tabtype choose_tabtype
= {
231 choose_properties_sensitive
,
232 choose_selectall_sensitive
,
233 choose_selectnone_sensitive
,
234 choose_properties_activate
,
235 choose_selectall_activate
,
236 choose_selectnone_activate
,
241 /** @brief Called when a mouse button is pressed or released */
242 gboolean
choose_button_event(GtkWidget
attribute((unused
)) *widget
,
243 GdkEventButton
*event
,
244 gpointer
attribute((unused
)) user_data
) {
245 if(event
->type
== GDK_BUTTON_RELEASE
&& event
->button
== 2) {
246 /* Middle click release - play track */
247 ensure_selected(GTK_TREE_VIEW(choose_view
), event
);
248 choose_play_activate(NULL
, NULL
);
249 } else if(event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
250 /* Right click press - pop up the menu */
251 ensure_selected(GTK_TREE_VIEW(choose_view
), event
);
252 popup(&choose_menu
, event
,
253 choose_menuitems
, sizeof choose_menuitems
/ sizeof *choose_menuitems
,