make all use of bind/mem_fun be explicitly sigc::
[ardour.git] / gtk2_ardour / playlist_selector.cc
1 /*
2     Copyright (C) 2004 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18
19 */
20
21 #include <gtkmm/button.h>
22
23 #include "ardour/session_playlist.h"
24 #include "ardour/audio_diskstream.h"
25 #include "ardour/playlist.h"
26 #include "ardour/audio_track.h"
27 #include "ardour/audioplaylist.h"
28 #include "ardour/configuration.h"
29
30 #include <gtkmm2ext/gtk_ui.h>
31
32 #include "playlist_selector.h"
33 #include "route_ui.h"
34 #include "gui_thread.h"
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace sigc;
40 using namespace Gtk;
41 using namespace Gtkmm2ext;
42 using namespace ARDOUR;
43 using namespace PBD;
44
45 PlaylistSelector::PlaylistSelector ()
46         : ArdourDialog ("playlist selector")
47 {
48         rui = 0;
49
50         set_position (WIN_POS_MOUSE);
51         set_name ("PlaylistSelectorWindow");
52         set_modal(true);
53         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
54         set_size_request (300, 200);
55
56         set_title (_("Playlists"));
57
58         model = TreeStore::create (columns);
59         tree.set_model (model);
60         tree.append_column (_("Playlists grouped by track"), columns.text);
61
62         scroller.add (tree);
63         scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
64
65         get_vbox()->set_border_width (6);
66         get_vbox()->set_spacing (12);
67
68         get_vbox()->pack_start (scroller);
69
70         Button* b = add_button (_("close"), RESPONSE_CANCEL);
71         b->signal_clicked().connect (sigc::mem_fun(*this, &PlaylistSelector::close_button_click));
72
73 }
74
75 PlaylistSelector::~PlaylistSelector ()
76 {
77         clear_map ();
78 }
79
80 void
81 PlaylistSelector::clear_map ()
82 {
83         for (DSPL_Map::iterator x = dspl_map.begin(); x != dspl_map.end(); ++x) {
84                 delete x->second;
85         }
86         dspl_map.clear ();
87 }
88
89 bool
90 PlaylistSelector::on_unmap_event (GdkEventAny* ev)
91 {
92         clear_map ();
93         if (model) {
94                 model->clear ();
95         }
96         return Dialog::on_unmap_event (ev);
97 }
98
99 void
100 PlaylistSelector::show_for (RouteUI* ruix)
101 {
102         vector<const char*> item;
103         boost::shared_ptr<Diskstream> this_ds;
104         string str;
105
106         rui = ruix;
107
108         set_title (string_compose (_("Playlist for %1"), rui->route()->name()));
109
110         clear_map ();
111         select_connection.disconnect ();
112
113         model->clear ();
114
115         session->playlists->foreach (this, &PlaylistSelector::add_playlist_to_map);
116
117         this_ds = rui->get_diskstream();
118
119         TreeModel::Row others = *(model->append ());
120
121         others[columns.text] = _("Other tracks");
122         boost::shared_ptr<Playlist> proxy = others[columns.playlist];
123         proxy.reset ();
124
125         for (DSPL_Map::iterator x = dspl_map.begin(); x != dspl_map.end(); ++x) {
126
127                 boost::shared_ptr<Diskstream> ds = session->diskstream_by_id (x->first);
128
129                 if (ds == 0) {
130                         continue;
131                 }
132
133                 /* add a node for the diskstream */
134
135                 string nodename;
136
137                 if (ds->name().empty()) {
138                         nodename = _("unassigned");
139                 } else {
140                         nodename = ds->name().c_str();
141                 }
142
143                 TreeModel::Row row;
144                 TreeModel::Row* selected_row = 0;
145                 TreePath this_path;
146
147                 if (ds == this_ds) {
148                         row = *(model->prepend());
149                         row[columns.text] = nodename;
150                         boost::shared_ptr<Playlist> proxy = row[columns.playlist];
151                         proxy.reset ();
152                 } else {
153                         row = *(model->append (others.children()));
154                         row[columns.text] = nodename;
155                         boost::shared_ptr<Playlist> proxy = row[columns.playlist];
156                         proxy.reset ();
157                 }
158
159                 /* Now insert all the playlists for this diskstream/track in a subtree */
160
161                 list<boost::shared_ptr<Playlist> > *pls = x->second;
162
163                 for (list<boost::shared_ptr<Playlist> >::iterator p = pls->begin(); p != pls->end(); ++p) {
164
165                         TreeModel::Row child_row;
166
167                         child_row = *(model->append (row.children()));
168                         child_row[columns.text] = (*p)->name();
169                         child_row[columns.playlist] = *p;
170
171                         if (*p == this_ds->playlist()) {
172                                 selected_row = &child_row;
173                         }
174                 }
175
176                 if (selected_row != 0) {
177                         tree.get_selection()->select (*selected_row);
178                 }
179         }
180
181         // Add unassigned (imported) playlists to the list
182         list<boost::shared_ptr<Playlist> > unassigned;
183         session->playlists->unassigned (unassigned);
184
185         TreeModel::Row row;
186         TreeModel::Row* selected_row = 0;
187         TreePath this_path;
188
189         row = *(model->append (others.children()));
190         row[columns.text] = _("Imported");
191         proxy = row[columns.playlist];
192         proxy.reset ();
193
194         for (list<boost::shared_ptr<Playlist> >::iterator p = unassigned.begin(); p != unassigned.end(); ++p) {
195                 TreeModel::Row child_row;
196
197                 child_row = *(model->append (row.children()));
198                 child_row[columns.text] = (*p)->name();
199                 child_row[columns.playlist] = *p;
200
201                 if (*p == this_ds->playlist()) {
202                         selected_row = &child_row;
203                 }
204
205                 if (selected_row != 0) {
206                         tree.get_selection()->select (*selected_row);
207                 }
208         }
209
210         show_all ();
211         select_connection = tree.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &PlaylistSelector::selection_changed));
212 }
213
214 void
215 PlaylistSelector::add_playlist_to_map (boost::shared_ptr<Playlist> pl)
216 {
217         boost::shared_ptr<AudioPlaylist> apl;
218
219         if (pl->frozen()) {
220                 return;
221         }
222
223         if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl)) == 0) {
224                 return;
225         }
226
227         DSPL_Map::iterator x;
228
229         if ((x = dspl_map.find (apl->get_orig_diskstream_id())) == dspl_map.end()) {
230
231                 pair<PBD::ID,list<boost::shared_ptr<Playlist> >*> newp (apl->get_orig_diskstream_id(), new list<boost::shared_ptr<Playlist> >);
232
233                 x = dspl_map.insert (dspl_map.end(), newp);
234         }
235
236         x->second->push_back (pl);
237 }
238
239 void
240 PlaylistSelector::set_session (Session* s)
241 {
242         ENSURE_GUI_THREAD (*this, &PlaylistSelector::set_session, s)
243
244         session = s;
245
246         if (session) {
247                 session->GoingAway.connect (sigc::bind (sigc::mem_fun(*this, &PlaylistSelector::set_session), static_cast<Session*> (0)));
248         }
249 }
250
251 void
252 PlaylistSelector::close_button_click ()
253 {
254         rui = 0;
255         hide ();
256 }
257
258 void
259 PlaylistSelector::selection_changed ()
260 {
261         boost::shared_ptr<Playlist> playlist;
262
263         TreeModel::iterator iter = tree.get_selection()->get_selected();
264
265         if (!iter || rui == 0) {
266                 /* nothing selected */
267                 return;
268         }
269
270         if ((playlist = ((*iter)[columns.playlist])) != 0) {
271
272                 boost::shared_ptr<AudioTrack> at;
273                 boost::shared_ptr<AudioPlaylist> apl;
274
275                 if ((at = rui->audio_track()) == 0) {
276                         /* eh? */
277                         return;
278                 }
279
280                 if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (playlist)) == 0) {
281                         /* eh? */
282                         return;
283                 }
284
285                 at->diskstream()->use_playlist (apl);
286
287                 hide ();
288         }
289
290 }
291