packaging tweaks for OS X
[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                 /* legacy sessions stored the diskstream ID as the original
126                  * playlist owner. so try there instead.
127                  */
128
129                 if (tr == 0) {
130                         tr = _session->track_by_diskstream_id (x->first);
131                 }
132
133                 if (tr == 0) {
134                         continue;
135                 }
136
137                 /* add a node for the track */
138
139                 string nodename;
140
141                 if (tr->name().empty()) {
142                         nodename = _("unassigned");
143                 } else {
144                         nodename = tr->name().c_str();
145                 }
146
147                 TreeModel::Row row;
148                 TreeModel::Row selected_row;
149                 bool have_selected = false;
150                 TreePath this_path;
151
152                 if (tr == this_track) {
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_track->playlist()) {
177                                 selected_row = child_row;
178                                 have_selected = true;
179                         }
180                 }
181
182                 if (have_selected) {
183                         tree.get_selection()->select (selected_row);
184                 }
185         }
186
187         // Add unassigned (imported) playlists to the list
188         list<boost::shared_ptr<Playlist> > unassigned;
189         _session->playlists->unassigned (unassigned);
190
191         TreeModel::Row row;
192         TreeModel::Row selected_row;
193         bool have_selected = false;
194         TreePath this_path;
195
196         row = *(model->append (others.children()));
197         row[columns.text] = _("Imported");
198         proxy = row[columns.playlist];
199         proxy.reset ();
200
201         for (list<boost::shared_ptr<Playlist> >::iterator p = unassigned.begin(); p != unassigned.end(); ++p) {
202                 TreeModel::Row child_row;
203
204                 child_row = *(model->append (row.children()));
205                 child_row[columns.text] = (*p)->name();
206                 child_row[columns.playlist] = *p;
207
208                 if (*p == this_track->playlist()) {
209                         selected_row = child_row;
210                         have_selected = true;
211                 }
212
213                 if (have_selected) {
214                         tree.get_selection()->select (selected_row);
215                 }
216         }
217
218         show_all ();
219         select_connection = tree.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &PlaylistSelector::selection_changed));
220 }
221
222 void
223 PlaylistSelector::add_playlist_to_map (boost::shared_ptr<Playlist> pl)
224 {
225         boost::shared_ptr<AudioPlaylist> apl;
226
227         if (pl->frozen()) {
228                 return;
229         }
230
231         if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl)) == 0) {
232                 return;
233         }
234
235         TrackPlaylistMap::iterator x;
236
237         if ((x = trpl_map.find (apl->get_orig_track_id())) == trpl_map.end()) {
238                 x = trpl_map.insert (trpl_map.end(), make_pair (apl->get_orig_track_id(), new list<boost::shared_ptr<Playlist> >));
239         }
240
241         x->second->push_back (pl);
242 }
243
244 void
245 PlaylistSelector::close_button_click ()
246 {
247         rui = 0;
248         hide ();
249 }
250
251 void
252 PlaylistSelector::selection_changed ()
253 {
254         boost::shared_ptr<Playlist> playlist;
255
256         TreeModel::iterator iter = tree.get_selection()->get_selected();
257
258         if (!iter || rui == 0) {
259                 /* nothing selected */
260                 return;
261         }
262
263         if ((playlist = ((*iter)[columns.playlist])) != 0) {
264
265                 boost::shared_ptr<AudioTrack> at;
266                 boost::shared_ptr<AudioPlaylist> apl;
267
268                 if ((at = rui->audio_track()) == 0) {
269                         /* eh? */
270                         return;
271                 }
272
273                 if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (playlist)) == 0) {
274                         /* eh? */
275                         return;
276                 }
277
278                 at->use_playlist (apl);
279
280                 hide ();
281         }
282
283 }
284