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