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