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