sort track selection into presentation order before duplicating.
[ardour.git] / gtk2_ardour / duplicate_routes_dialog.cc
1 /*
2     Copyright (C) 2015 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 #include "gtkmm/stock.h"
21
22 #include "ardour/route.h"
23 #include "ardour/session.h"
24
25 #include "ardour_ui.h"
26 #include "editor.h"
27 #include "duplicate_routes_dialog.h"
28 #include "selection.h"
29
30 #include "pbd/i18n.h"
31
32 using namespace ARDOUR;
33 using namespace Gtk;
34
35 DuplicateRouteDialog::DuplicateRouteDialog ()
36         : ArdourDialog (_("Duplicate Tracks & Busses"), false, false)
37         , playlist_option_label (_("For each Track:"))
38         , copy_playlists_button (playlist_button_group, _("Copy playlist"))
39         , new_playlists_button (playlist_button_group, _("New playlist"))
40         , share_playlists_button (playlist_button_group, _("Share playlist"))
41         , count_adjustment (1.0, 1.0, 999, 1.0, 10.0)
42         , count_spinner (count_adjustment)
43         , count_label (_("Duplicate each track/bus this number of times:"))
44 {
45         count_box.pack_start (count_label, false, false);
46         count_box.pack_start (count_spinner, false, false, 5);
47         get_vbox()->pack_start (count_box, false, false, 10);
48
49         Gtk::HBox* hb = manage (new HBox);
50         hb->pack_start (playlist_option_label, false, false);
51         get_vbox()->pack_start (*hb, false, false, 10);
52
53         playlist_button_box.pack_start (copy_playlists_button, false, false);
54         playlist_button_box.pack_start (new_playlists_button, false, false);
55         playlist_button_box.pack_start (share_playlists_button, false, false);
56         playlist_button_box.show_all ();
57
58         insert_at_combo.append_text (_("First"));
59         insert_at_combo.append_text (_("Before Selection"));
60         insert_at_combo.append_text (_("After Selection"));
61         insert_at_combo.append_text (_("Last"));
62         insert_at_combo.set_active (3);
63
64         Gtk::Label* l = manage (new Label (_("Insert duplicates at: ")));
65         Gtk::HBox* b = manage (new HBox);
66         b->pack_start (*l, false, false, 10);
67         b->pack_start (insert_at_combo, true, true);
68
69         get_vbox()->pack_end (*b, false, false, 10);
70
71         get_vbox()->show_all ();
72
73         add_button (Stock::CANCEL, RESPONSE_CANCEL);
74         add_button (Stock::OK, RESPONSE_OK);
75 }
76
77 int
78 DuplicateRouteDialog::restart (Session* s)
79 {
80         if (!s) {
81                 return -1;
82         }
83
84         set_session (s);
85
86         TrackSelection& tracks  (PublicEditor::instance().get_selection().tracks);
87         uint32_t ntracks = 0;
88         uint32_t nbusses = 0;
89
90         for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
91
92                 RouteUI* rui = dynamic_cast<RouteUI*> (*t);
93
94                 if (!rui) {
95                         /* some other type of timeaxis view, not a route */
96                         continue;
97                 }
98
99                 boost::shared_ptr<Route> r (rui->route());
100
101                 if (boost::dynamic_pointer_cast<Track> (r)) {
102                         ntracks++;
103                 } else {
104                         if (!r->is_master() && !r->is_monitor()) {
105                                 nbusses++;
106                         }
107                 }
108         }
109
110         if (ntracks == 0 && nbusses == 0) {
111                 std::cerr << "You can't do this\n";
112                 return -1;
113         }
114
115         /* XXX grrr. Gtk Boxes do not shrink when children are removed,
116            which is what we really want to happen here.
117         */
118
119         if (playlist_button_box.get_parent()) {
120                 get_vbox()->remove (playlist_button_box);
121         }
122
123         if (ntracks > 0) {
124                 get_vbox()->pack_end (playlist_button_box, false, false);
125         }
126
127         return 0;
128 }
129
130 uint32_t
131 DuplicateRouteDialog::count() const
132 {
133         return count_adjustment.get_value ();
134 }
135
136 ARDOUR::PlaylistDisposition
137 DuplicateRouteDialog::playlist_disposition() const
138 {
139         if (new_playlists_button.get_active()) {
140                 return ARDOUR::NewPlaylist;
141         } else if (copy_playlists_button.get_active()) {
142                 return ARDOUR::CopyPlaylist;
143         }
144
145         return ARDOUR::SharePlaylist;
146 }
147
148 void
149 DuplicateRouteDialog::on_response (int response)
150 {
151         hide ();
152
153         if (response != RESPONSE_OK) {
154                 return;
155         }
156
157         ARDOUR::PlaylistDisposition playlist_action = playlist_disposition ();
158         uint32_t cnt = count ();
159
160         /* Copy the track selection because it will/may change as we add new ones */
161         TrackSelection tracks  (PublicEditor::instance().get_selection().tracks);
162         int err = 0;
163
164         /* Track Selection should be sorted into presentation order before
165          * duplicating, so that new tracks appear in same order as the
166          * originals.
167          */
168
169         StripableList sl;
170
171         for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
172                 RouteUI* rui = dynamic_cast<RouteUI*> (*t);
173                 sl.push_back (rui->route());
174         }
175
176         sl.sort (Stripable::Sorter());
177
178         for (StripableList::iterator s = sl.begin(); s != sl.end(); ++s) {
179
180                 boost::shared_ptr<Route> r;
181
182                 if ((r = boost::dynamic_pointer_cast<Route> (*s)) == 0) {
183                         /* some other type of Stripable, not a route */
184                         continue;
185                 }
186
187                 if ((*s)->is_master() || (*s)->is_monitor()) {
188                         /* no option to duplicate these */
189                         continue;
190                 }
191
192                 XMLNode& state (r->get_state());
193                 RouteList rl = _session->new_route_from_template (cnt, ARDOUR_UI::instance()->translate_order (insert_at()), state, std::string(), playlist_action);
194
195                 /* normally the state node would be added to a parent, and
196                  * ownership would transfer. Because we don't do that here,
197                  * we need to delete the node ourselves.
198                  */
199
200                 delete &state;
201
202                 if (rl.empty()) {
203                         err++;
204                         break;
205                 }
206         }
207
208         if (err) {
209                 MessageDialog msg (_("1 or more tracks/busses could not be duplicated"),
210                                      true, MESSAGE_ERROR, BUTTONS_OK, true);
211                 msg.set_position (WIN_POS_MOUSE);
212                 msg.run ();
213         }
214 }
215
216 RouteDialogs::InsertAt
217 DuplicateRouteDialog::insert_at ()
218 {
219         using namespace RouteDialogs;
220
221         std::string str = insert_at_combo.get_active_text();
222
223         if (str == _("First")) {
224                 return First;
225         } else if (str == _("After Selection")) {
226                 return AfterSelection;
227         } else if (str == _("Before Selection")){
228                 return BeforeSelection;
229         }
230         return Last;
231 }