add sampo's synthesize_sources perl script to tools; add scroll-playhead-{forward...
[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     $Id$
19
20 */
21
22 #include <gtkmm/button.h>
23
24 #include <ardour/session_playlist.h>
25 #include <ardour/audio_diskstream.h>
26 #include <ardour/playlist.h>
27 #include <ardour/audio_track.h>
28 #include <ardour/audioplaylist.h>
29 #include <ardour/configuration.h>
30
31 #include <gtkmm2ext/gtk_ui.h>
32
33 #include "playlist_selector.h"
34 #include "route_ui.h"
35 #include "gui_thread.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace sigc;
41 using namespace Gtk;
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_title (_("ardour: playlists"));
53         set_modal(true);
54         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
55         set_size_request (300, 200);
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 (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         str = _("ardour: playlist for ");
108         str += rui->route()->name();
109
110         set_title (str);
111
112         clear_map ();
113         select_connection.disconnect ();
114
115         model->clear ();
116         
117         session->foreach_playlist (this, &PlaylistSelector::add_playlist_to_map);
118
119         this_ds = rui->get_diskstream();
120
121         TreeModel::Row others = *(model->append ());
122
123         others[columns.text] = _("Other tracks");
124         boost::shared_ptr<Playlist> proxy = others[columns.playlist];
125         proxy.reset ();
126         
127         for (DSPL_Map::iterator x = dspl_map.begin(); x != dspl_map.end(); ++x) {
128
129                 boost::shared_ptr<Diskstream> ds = session->diskstream_by_id (x->first);
130
131                 if (ds == 0) {
132                         continue;
133                 }
134
135                 /* add a node for the diskstream */
136
137                 string nodename;
138
139                 if (ds->name().empty()) {
140                         nodename = _("unassigned");
141                 } else {
142                         nodename = ds->name().c_str();
143                 }
144                 
145                 TreeModel::Row row;
146                 TreeModel::Row* selected_row = 0;
147                 TreePath this_path;
148
149                 if (ds == this_ds) {
150                         row = *(model->prepend());
151                         row[columns.text] = nodename;
152                         boost::shared_ptr<Playlist> proxy = row[columns.playlist];
153                         proxy.reset ();
154                 } else {
155                         row = *(model->append (others.children()));
156                         row[columns.text] = nodename;
157                         boost::shared_ptr<Playlist> proxy = row[columns.playlist];
158                         proxy.reset ();
159                 }
160
161                 /* Now insert all the playlists for this diskstream/track in a subtree */
162                 
163                 list<boost::shared_ptr<Playlist> > *pls = x->second;
164                 
165                 for (list<boost::shared_ptr<Playlist> >::iterator p = pls->begin(); p != pls->end(); ++p) {
166
167                         TreeModel::Row child_row;
168
169                         child_row = *(model->append (row.children()));
170                         child_row[columns.text] = (*p)->name();
171                         child_row[columns.playlist] = *p;
172
173                         if (*p == this_ds->playlist()) {
174                                 selected_row = &child_row;
175                         } 
176                 }
177                 
178                 if (selected_row != 0) {
179                         tree.get_selection()->select (*selected_row);
180                 }
181         }
182
183         show_all ();
184         select_connection = tree.get_selection()->signal_changed().connect (mem_fun(*this, &PlaylistSelector::selection_changed));
185 }
186
187 void
188 PlaylistSelector::add_playlist_to_map (boost::shared_ptr<Playlist> pl)
189 {
190         boost::shared_ptr<AudioPlaylist> apl;
191
192         if (pl->frozen()) {
193                 return;
194         }
195         
196         if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl)) == 0) {
197                 return;
198         }
199
200         DSPL_Map::iterator x;
201
202         if ((x = dspl_map.find (apl->get_orig_diskstream_id())) == dspl_map.end()) {
203
204                 pair<PBD::ID,list<boost::shared_ptr<Playlist> >*> newp (apl->get_orig_diskstream_id(), new list<boost::shared_ptr<Playlist> >);
205                 
206                 x = dspl_map.insert (dspl_map.end(), newp);
207         }
208
209         x->second->push_back (pl);
210 }
211
212 void
213 PlaylistSelector::set_session (Session* s)
214 {
215         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PlaylistSelector::set_session), s));
216
217         session = s;
218
219         if (session) {
220                 session->GoingAway.connect (bind (mem_fun(*this, &PlaylistSelector::set_session), static_cast<Session*> (0)));
221         }
222 }
223
224 void
225 PlaylistSelector::close_button_click ()
226 {
227         rui = 0;
228         hide ();
229 }
230
231 void
232 PlaylistSelector::selection_changed ()
233 {
234         boost::shared_ptr<Playlist> playlist;
235
236         TreeModel::iterator iter = tree.get_selection()->get_selected();
237
238         if (!iter || rui == 0) {
239                 /* nothing selected */
240                 return;
241         }
242
243         if ((playlist = ((*iter)[columns.playlist])) != 0) {
244                 
245                 AudioTrack* at;
246                 boost::shared_ptr<AudioPlaylist> apl;
247                 
248                 if ((at = rui->audio_track()) == 0) {
249                         /* eh? */
250                         return;
251                 }
252                 
253                 if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (playlist)) == 0) {
254                         /* eh? */
255                         return;
256                 }
257                 
258                 at->diskstream()->use_playlist (apl);
259
260                 hide ();
261         }
262
263 }
264