chiark / gitweb /
Make queue columns reorderable
[disorder] / disobedience / queue-menu.c
CommitLineData
c133bd3c
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2006-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 "queue-generic.h"
22
23/* Select All */
24
25int ql_selectall_sensitive(struct queuelike *ql) {
26 return !!ql->q;
27}
28
29void ql_selectall_activate(GtkMenuItem attribute((unused)) *menuitem,
30 gpointer user_data) {
31 struct queuelike *ql = user_data;
32
33 gtk_tree_selection_select_all(ql->selection);
34}
35
36/* Select None */
37
38int ql_selectnone_sensitive(struct queuelike *ql) {
39 return gtk_tree_selection_count_selected_rows(ql->selection) > 0;
40}
41
42void ql_selectnone_activate(GtkMenuItem attribute((unused)) *menuitem,
43 gpointer user_data) {
44 struct queuelike *ql = user_data;
45
46 gtk_tree_selection_unselect_all(ql->selection);
47}
48
49/* Properties */
50
51int ql_properties_sensitive(struct queuelike *ql) {
52 return gtk_tree_selection_count_selected_rows(ql->selection) > 0;
53}
54
55void ql_properties_activate(GtkMenuItem attribute((unused)) *menuitem,
56 gpointer user_data) {
57 struct queuelike *ql = user_data;
58 struct vector v[1];
59 GtkTreeIter iter[1];
60
61 vector_init(v);
62 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
63 for(struct queue_entry *q = ql->q; q; q = q->next) {
64 if(gtk_tree_selection_iter_is_selected(ql->selection, iter))
65 vector_append(v, (char *)q->track);
66 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
67 }
68 if(v->nvec)
69 properties(v->nvec, (const char **)v->vec);
70}
71
72/* Scratch */
73
74int ql_scratch_sensitive(struct queuelike attribute((unused)) *ql) {
c9fb35f4
RK
75 return !!(last_state & DISORDER_PLAYING)
76 && right_scratchable(last_rights, config->username, playing_track);
77}
78
ac56170f
RK
79static void ql_scratch_completed(void attribute((unused)) *v,
80 const char *error) {
c9fb35f4
RK
81 if(error)
82 popup_protocol_error(0, error);
c133bd3c
RK
83}
84
85void ql_scratch_activate(GtkMenuItem attribute((unused)) *menuitem,
86 gpointer attribute((unused)) user_data) {
ac56170f 87 disorder_eclient_scratch_playing(client, ql_scratch_completed, 0);
c133bd3c
RK
88}
89
90/* Remove */
91
ac56170f
RK
92static void ql_remove_sensitive_callback(GtkTreeModel *model,
93 GtkTreePath attribute((unused)) *path,
94 GtkTreeIter *iter,
95 gpointer data) {
22717074
RK
96 struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql");
97 struct queue_entry *q = ql_iter_to_q(ql, iter);
98 const int removable = (q != playing_track
99 && right_removable(last_rights, config->username, q));
100 int *const counts = data;
101 ++counts[removable];
102}
103
c133bd3c 104int ql_remove_sensitive(struct queuelike *ql) {
22717074
RK
105 int counts[2] = { 0, 0 };
106 gtk_tree_selection_selected_foreach(ql->selection,
ac56170f 107 ql_remove_sensitive_callback,
22717074
RK
108 counts);
109 /* Remove will work if we have at least some removable tracks selected, and
110 * no unremovable ones */
111 return counts[1] > 0 && counts[0] == 0;
112}
113
ac56170f
RK
114static void ql_remove_completed(void attribute((unused)) *v,
115 const char *error) {
22717074
RK
116 if(error)
117 popup_protocol_error(0, error);
118}
119
ac56170f
RK
120static void ql_remove_activate_callback(GtkTreeModel *model,
121 GtkTreePath attribute((unused)) *path,
122 GtkTreeIter *iter,
123 gpointer attribute((unused)) data) {
22717074
RK
124 struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql");
125 struct queue_entry *q = ql_iter_to_q(ql, iter);
126
ac56170f 127 disorder_eclient_remove(client, q->id, ql_remove_completed, q);
c133bd3c
RK
128}
129
130void ql_remove_activate(GtkMenuItem attribute((unused)) *menuitem,
22717074
RK
131 gpointer user_data) {
132 struct queuelike *ql = user_data;
133 gtk_tree_selection_selected_foreach(ql->selection,
ac56170f 134 ql_remove_activate_callback,
22717074 135 0);
c133bd3c
RK
136}
137
138/* Play */
139
140int ql_play_sensitive(struct queuelike *ql) {
ac56170f
RK
141 return (last_rights & RIGHT_PLAY)
142 && gtk_tree_selection_count_selected_rows(ql->selection) > 0;
c133bd3c
RK
143}
144
ac56170f
RK
145static void ql_play_completed(void attribute((unused)) *v, const char *error) {
146 if(error)
147 popup_protocol_error(0, error);
c133bd3c
RK
148}
149
ac56170f
RK
150static void ql_play_activate_callback(GtkTreeModel *model,
151 GtkTreePath attribute((unused)) *path,
152 GtkTreeIter *iter,
153 gpointer attribute((unused)) data) {
154 struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql");
155 struct queue_entry *q = ql_iter_to_q(ql, iter);
156
157 disorder_eclient_play(client, q->track, ql_play_completed, q);
158}
159
160void ql_play_activate(GtkMenuItem attribute((unused)) *menuitem,
161 gpointer user_data) {
162 struct queuelike *ql = user_data;
163 gtk_tree_selection_selected_foreach(ql->selection,
164 ql_play_activate_callback,
165 0);
166}
c133bd3c
RK
167
168/** @brief Create @c ql->menu if it does not already exist */
169static void ql_create_menu(struct queuelike *ql) {
170 if(ql->menu)
171 return;
172 ql->menu = gtk_menu_new();
173 g_signal_connect(ql->menu, "destroy",
174 G_CALLBACK(gtk_widget_destroyed), &ql->menu);
175 for(int n = 0; n < ql->nmenuitems; ++n) {
176 ql->menuitems[n].w = gtk_menu_item_new_with_label(ql->menuitems[n].name);
177 gtk_menu_attach(GTK_MENU(ql->menu), ql->menuitems[n].w, 0, 1, n, n + 1);
178 }
179 set_tool_colors(ql->menu);
180}
181
182/** @brief Configure @c ql->menu */
183static void ql_configure_menu(struct queuelike *ql) {
184 /* Set the sensitivity of each menu item and (re-)establish the signal
185 * handlers */
186 for(int n = 0; n < ql->nmenuitems; ++n) {
187 if(ql->menuitems[n].handlerid)
188 g_signal_handler_disconnect(ql->menuitems[n].w,
189 ql->menuitems[n].handlerid);
190 gtk_widget_set_sensitive(ql->menuitems[n].w,
191 ql->menuitems[n].sensitive(ql));
192 ql->menuitems[n].handlerid = g_signal_connect
193 (ql->menuitems[n].w, "activate",
194 G_CALLBACK(ql->menuitems[n].activate), ql);
195 }
196}
197
198/** @brief Called when a button is released over a queuelike */
be909398 199gboolean ql_button_release(GtkWidget*widget,
c133bd3c
RK
200 GdkEventButton *event,
201 gpointer user_data) {
202 struct queuelike *ql = user_data;
203
204 if(event->type == GDK_BUTTON_PRESS
205 && event->button == 3) {
be909398
RK
206 /* Right button click. */
207 if(gtk_tree_selection_count_selected_rows(ql->selection) == 0) {
208 /* Nothing is selected, select whatever is under the pointer */
209 GtkTreePath *path;
210 if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget),
211 event->x, event->y,
212 &path,
213 NULL,
214 NULL, NULL))
215 gtk_tree_selection_select_path(ql->selection, path);
216 }
c133bd3c
RK
217 ql_create_menu(ql);
218 ql_configure_menu(ql);
219 gtk_widget_show_all(ql->menu);
220 gtk_menu_popup(GTK_MENU(ql->menu), 0, 0, 0, 0,
221 event->button, event->time);
222 return TRUE; /* hide the click from other widgets */
223 }
224
225 return FALSE;
226}
227
ee7552f8
RK
228static int ql_tab_selectall_sensitive(void *extra) {
229 return ql_selectall_sensitive(extra);
230}
231
232static void ql_tab_selectall_activate(void *extra) {
233 ql_selectall_activate(NULL, extra);
234}
235
236static int ql_tab_selectnone_sensitive(void *extra) {
237 return ql_selectnone_sensitive(extra);
238}
239
240static void ql_tab_selectnone_activate(void *extra) {
241 ql_selectnone_activate(NULL, extra);
242}
243
244static int ql_tab_properties_sensitive(void *extra) {
245 return ql_properties_sensitive(extra);
246}
247
248static void ql_tab_properties_activate(void *extra) {
249 ql_properties_activate(NULL, extra);
250}
251
252struct tabtype *ql_tabtype(struct queuelike *ql) {
253 static const struct tabtype ql_tabtype = {
254 ql_tab_properties_sensitive,
255 ql_tab_selectall_sensitive,
256 ql_tab_selectnone_sensitive,
257 ql_tab_properties_activate,
258 ql_tab_selectall_activate,
259 ql_tab_selectnone_activate,
260 0,
261 0
262 };
263
264 struct tabtype *t = xmalloc(sizeof *t);
265 *t = ql_tabtype;
266 t->extra = ql;
267 return t;
268}
269
c133bd3c
RK
270/*
271Local Variables:
272c-basic-offset:2
273comment-column:40
274fill-column:79
275indent-tabs-mode:nil
276End:
277*/