switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[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 Gtk;
40 using namespace Gtkmm2ext;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 PlaylistSelector::PlaylistSelector ()
45         : ArdourDialog ("playlist selector")
46 {
47         rui = 0;
48
49         set_position (WIN_POS_MOUSE);
50         set_name ("PlaylistSelectorWindow");
51         set_modal(true);
52         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
53         set_size_request (300, 200);
54
55         set_title (_("Playlists"));
56
57         model = TreeStore::create (columns);
58         tree.set_model (model);
59         tree.append_column (_("Playlists grouped by track"), columns.text);
60
61         scroller.add (tree);
62         scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
63
64         get_vbox()->set_border_width (6);
65         get_vbox()->set_spacing (12);
66
67         get_vbox()->pack_start (scroller);
68
69         Button* b = add_button (_("close"), RESPONSE_CANCEL);
70         b->signal_clicked().connect (sigc::mem_fun(*this, &PlaylistSelector::close_button_click));
71
72 }
73
74 PlaylistSelector::~PlaylistSelector ()
75 {
76         clear_map ();
77 }
78
79 void
80 PlaylistSelector::clear_map ()
81 {
82         for (DSPL_Map::iterator x = dspl_map.begin(); x != dspl_map.end(); ++x) {
83                 delete x->second;
84         }
85         dspl_map.clear ();
86 }
87
88 bool
89 PlaylistSelector::on_unmap_event (GdkEventAny* ev)
90 {
91         clear_map ();
92         if (model) {
93                 model->clear ();
94         }
95         return Dialog::on_unmap_event (ev);
96 }
97
98 void
99 PlaylistSelector::show_for (RouteUI* ruix)
100 {
101         vector<const char*> item;
102         boost::shared_ptr<Diskstream> this_ds;
103         string str;
104
105         rui = ruix;
106
107         set_title (string_compose (_("Playlist for %1"), rui->route()->name()));
108
109         clear_map ();
110         select_connection.disconnect ();
111
112         model->clear ();
113
114         _session->playlists->foreach (this, &PlaylistSelector::add_playlist_to_map);
115
116         this_ds = rui->get_diskstream();
117
118         TreeModel::Row others = *(model->append ());
119
120         others[columns.text] = _("Other tracks");
121         boost::shared_ptr<Playlist> proxy = others[columns.playlist];
122         proxy.reset ();
123
124         for (DSPL_Map::iterator x = dspl_map.begin(); x != dspl_map.end(); ++x) {
125
126                 boost::shared_ptr<Diskstream> ds = _session->diskstream_by_id (x->first);
127
128                 if (ds == 0) {
129                         continue;
130                 }
131
132                 /* add a node for the diskstream */
133
134                 string nodename;
135
136                 if (ds->name().empty()) {
137                         nodename = _("unassigned");
138                 } else {
139                         nodename = ds->name().c_str();
140                 }
141
142                 TreeModel::Row row;
143                 TreeModel::Row* selected_row = 0;
144                 TreePath this_path;
145
146                 if (ds == this_ds) {
147                         row = *(model->prepend());
148                         row[columns.text] = nodename;
149                         boost::shared_ptr<Playlist> proxy = row[columns.playlist];
150                         proxy.reset ();
151                 } else {
152                         row = *(model->append (others.children()));
153                         row[columns.text] = nodename;
154                         boost::shared_ptr<Playlist> proxy = row[columns.playlist];
155                         proxy.reset ();
156                 }
157
158                 /* Now insert all the playlists for this diskstream/track in a subtree */
159
160                 list<boost::shared_ptr<Playlist> > *pls = x->second;
161
162                 for (list<boost::shared_ptr<Playlist> >::iterator p = pls->begin(); p != pls->end(); ++p) {
163
164                         TreeModel::Row child_row;
165
166                         child_row = *(model->append (row.children()));
167                         child_row[columns.text] = (*p)->name();
168                         child_row[columns.playlist] = *p;
169
170                         if (*p == this_ds->playlist()) {
171                                 selected_row = &child_row;
172                         }
173                 }
174
175                 if (selected_row != 0) {
176                         tree.get_selection()->select (*selected_row);
177                 }
178         }
179
180         // Add unassigned (imported) playlists to the list
181         list<boost::shared_ptr<Playlist> > unassigned;
182         _session->playlists->unassigned (unassigned);
183
184         TreeModel::Row row;
185         TreeModel::Row* selected_row = 0;
186         TreePath this_path;
187
188         row = *(model->append (others.children()));
189         row[columns.text] = _("Imported");
190         proxy = row[columns.playlist];
191         proxy.reset ();
192
193         for (list<boost::shared_ptr<Playlist> >::iterator p = unassigned.begin(); p != unassigned.end(); ++p) {
194                 TreeModel::Row child_row;
195
196                 child_row = *(model->append (row.children()));
197                 child_row[columns.text] = (*p)->name();
198                 child_row[columns.playlist] = *p;
199
200                 if (*p == this_ds->playlist()) {
201                         selected_row = &child_row;
202                 }
203
204                 if (selected_row != 0) {
205                         tree.get_selection()->select (*selected_row);
206                 }
207         }
208
209         show_all ();
210         select_connection = tree.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &PlaylistSelector::selection_changed));
211 }
212
213 void
214 PlaylistSelector::add_playlist_to_map (boost::shared_ptr<Playlist> pl)
215 {
216         boost::shared_ptr<AudioPlaylist> apl;
217
218         if (pl->frozen()) {
219                 return;
220         }
221
222         if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl)) == 0) {
223                 return;
224         }
225
226         DSPL_Map::iterator x;
227
228         if ((x = dspl_map.find (apl->get_orig_diskstream_id())) == dspl_map.end()) {
229
230                 pair<PBD::ID,list<boost::shared_ptr<Playlist> >*> newp (apl->get_orig_diskstream_id(), new list<boost::shared_ptr<Playlist> >);
231
232                 x = dspl_map.insert (dspl_map.end(), newp);
233         }
234
235         x->second->push_back (pl);
236 }
237
238 void
239 PlaylistSelector::close_button_click ()
240 {
241         rui = 0;
242         hide ();
243 }
244
245 void
246 PlaylistSelector::selection_changed ()
247 {
248         boost::shared_ptr<Playlist> playlist;
249
250         TreeModel::iterator iter = tree.get_selection()->get_selected();
251
252         if (!iter || rui == 0) {
253                 /* nothing selected */
254                 return;
255         }
256
257         if ((playlist = ((*iter)[columns.playlist])) != 0) {
258
259                 boost::shared_ptr<AudioTrack> at;
260                 boost::shared_ptr<AudioPlaylist> apl;
261
262                 if ((at = rui->audio_track()) == 0) {
263                         /* eh? */
264                         return;
265                 }
266
267                 if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (playlist)) == 0) {
268                         /* eh? */
269                         return;
270                 }
271
272                 at->diskstream()->use_playlist (apl);
273
274                 hide ();
275         }
276
277 }
278