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