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