chiark / gitweb /
Complete choose tab popup support (also middle-click).
[disorder] / disobedience / choose-menu.c
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 VECTOR_TYPE(cdvector, struct choosedata *, xrealloc);
25
26 /** @brief Popup menu */
27 static GtkWidget *choose_menu;
28
29 /** @brief Callback for choose_get_selected() */
30 static 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 */
42 static 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
55 static int choose_selectall_sensitive(void attribute((unused)) *extra) {
56   return TRUE;
57 }
58   
59 static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item,
60                                       gpointer attribute((unused)) userdata) {
61   gtk_tree_selection_select_all(choose_selection);
62 }
63   
64 static int choose_selectnone_sensitive(void attribute((unused)) *extra) {
65   return gtk_tree_selection_count_selected_rows(choose_selection) > 0;
66 }
67   
68 static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item,
69                                        gpointer attribute((unused)) userdata) {
70   gtk_tree_selection_unselect_all(choose_selection);
71 }
72   
73 static int choose_play_sensitive(void attribute((unused)) *extra) {
74   struct choosedata *cd, **cdp = choose_get_selected(NULL);
75   int counts[2] = { 0, 0 };
76   while((cd = *cdp++))
77     ++counts[cd->type];
78   return !counts[CHOOSE_DIRECTORY] && counts[CHOOSE_FILE];
79 }
80
81 static void choose_play_completed(void attribute((unused)) *v,
82                                   const char *error) {
83   if(error)
84     popup_protocol_error(0, error);
85 }
86   
87 static void choose_play_activate(GtkMenuItem attribute((unused)) *item,
88                                  gpointer attribute((unused)) userdata) {
89   struct choosedata *cd, **cdp = choose_get_selected(NULL);
90   while((cd = *cdp++)) {
91     if(cd->type == CHOOSE_FILE)
92       disorder_eclient_play(client, xstrdup(cd->track),
93                             choose_play_completed, 0);
94   }
95 }
96   
97 static int choose_properties_sensitive(void *extra) {
98   return choose_play_sensitive(extra);
99 }
100   
101 static void choose_properties_activate(GtkMenuItem attribute((unused)) *item,
102                                        gpointer attribute((unused)) userdata) {
103   struct choosedata *cd, **cdp = choose_get_selected(NULL);
104   struct vector v[1];
105   vector_init(v);
106   while((cd = *cdp++))
107     vector_append(v, xstrdup(cd->track));
108   properties(v->nvec, (const char **)v->vec);
109 }
110
111 /** @brief Pop-up menu for choose */
112 static struct menuitem choose_menuitems[] = {
113   {
114     "Play track",
115     choose_play_activate,
116     choose_play_sensitive,
117     0,
118     0
119   },
120   {
121     "Track properties",
122     choose_properties_activate,
123     choose_properties_sensitive,
124     0,
125     0
126   },
127   {
128     "Select all tracks",
129     choose_selectall_activate,
130     choose_selectall_sensitive,
131     0,
132     0
133   },
134   {
135     "Deselect all tracks",
136     choose_selectnone_activate,
137     choose_selectnone_sensitive,
138     0,
139     0
140   },
141 };
142
143 const struct tabtype choose_tabtype = {
144   choose_properties_sensitive,
145   choose_selectall_sensitive,
146   choose_selectnone_sensitive,
147   choose_properties_activate,
148   choose_selectall_activate,
149   choose_selectnone_activate,
150   0,
151   0
152 };
153
154 /** @brief Called when a mouse button is pressed or released */
155 gboolean choose_button_event(GtkWidget attribute((unused)) *widget,
156                              GdkEventButton *event,
157                              gpointer attribute((unused)) user_data) {
158   if(event->type == GDK_BUTTON_RELEASE && event->button == 2) {
159     /* Middle click release - play track */
160     ensure_selected(GTK_TREE_VIEW(choose_view), event);
161     choose_play_activate(NULL, NULL);
162   } else if(event->type == GDK_BUTTON_PRESS && event->button == 3) {
163     /* Right click press - pop up the menu */
164     ensure_selected(GTK_TREE_VIEW(choose_view), event);
165     popup(&choose_menu, event,
166           choose_menuitems, sizeof choose_menuitems / sizeof *choose_menuitems,
167           0);
168     return TRUE;
169   }
170   return FALSE;
171 }
172
173 /*
174 Local Variables:
175 c-basic-offset:2
176 comment-column:40
177 fill-column:79
178 indent-tabs-mode:nil
179 End:
180 */