centralize use of "from-context-menu" in Editor::get_preferred_edit_position(), and...
[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 = 0;
149                 TreePath this_path;
150
151                 if (tr == this_track) {
152                         row = *(model->prepend());
153                         row[columns.text] = nodename;
154                         boost::shared_ptr<Playlist> proxy = row[columns.playlist];
155                         proxy.reset ();
156                 } else {
157                         row = *(model->append (others.children()));
158                         row[columns.text] = nodename;
159                         boost::shared_ptr<Playlist> proxy = row[columns.playlist];
160                         proxy.reset ();
161                 }
162
163                 /* Now insert all the playlists for this diskstream/track in a subtree */
164
165                 list<boost::shared_ptr<Playlist> >* pls = x->second;
166
167                 for (list<boost::shared_ptr<Playlist> >::iterator p = pls->begin(); p != pls->end(); ++p) {
168
169                         TreeModel::Row child_row;
170
171                         child_row = *(model->append (row.children()));
172                         child_row[columns.text] = (*p)->name();
173                         child_row[columns.playlist] = *p;
174
175                         if (*p == this_track->playlist()) {
176                                 selected_row = &child_row;
177                         }
178                 }
179
180                 if (selected_row != 0) {
181                         tree.get_selection()->select (*selected_row);
182                 }
183         }
184
185         // Add unassigned (imported) playlists to the list
186         list<boost::shared_ptr<Playlist> > unassigned;
187         _session->playlists->unassigned (unassigned);
188
189         TreeModel::Row row;
190         TreeModel::Row* selected_row = 0;
191         TreePath this_path;
192
193         row = *(model->append (others.children()));
194         row[columns.text] = _("Imported");
195         proxy = row[columns.playlist];
196         proxy.reset ();
197
198         for (list<boost::shared_ptr<Playlist> >::iterator p = unassigned.begin(); p != unassigned.end(); ++p) {
199                 TreeModel::Row child_row;
200
201                 child_row = *(model->append (row.children()));
202                 child_row[columns.text] = (*p)->name();
203                 child_row[columns.playlist] = *p;
204
205                 if (*p == this_track->playlist()) {
206                         selected_row = &child_row;
207                 }
208
209                 if (selected_row != 0) {
210                         tree.get_selection()->select (*selected_row);
211                 }
212         }
213
214         show_all ();
215         select_connection = tree.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &PlaylistSelector::selection_changed));
216 }
217
218 void
219 PlaylistSelector::add_playlist_to_map (boost::shared_ptr<Playlist> pl)
220 {
221         boost::shared_ptr<AudioPlaylist> apl;
222
223         if (pl->frozen()) {
224                 return;
225         }
226
227         if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl)) == 0) {
228                 return;
229         }
230
231         TrackPlaylistMap::iterator x;
232
233         if ((x = trpl_map.find (apl->get_orig_track_id())) == trpl_map.end()) {
234                 x = trpl_map.insert (trpl_map.end(), make_pair (apl->get_orig_track_id(), new list<boost::shared_ptr<Playlist> >));
235         }
236
237         x->second->push_back (pl);
238 }
239
240 void
241 PlaylistSelector::close_button_click ()
242 {
243         rui = 0;
244         hide ();
245 }
246
247 void
248 PlaylistSelector::selection_changed ()
249 {
250         boost::shared_ptr<Playlist> playlist;
251
252         TreeModel::iterator iter = tree.get_selection()->get_selected();
253
254         if (!iter || rui == 0) {
255                 /* nothing selected */
256                 return;
257         }
258
259         if ((playlist = ((*iter)[columns.playlist])) != 0) {
260
261                 boost::shared_ptr<AudioTrack> at;
262                 boost::shared_ptr<AudioPlaylist> apl;
263
264                 if ((at = rui->audio_track()) == 0) {
265                         /* eh? */
266                         return;
267                 }
268
269                 if ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (playlist)) == 0) {
270                         /* eh? */
271                         return;
272                 }
273
274                 at->use_playlist (apl);
275
276                 hide ();
277         }
278
279 }
280