fix incorrect accumulation of export video options each time the dialog is used
[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         for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
165
166                 RouteUI* rui = dynamic_cast<RouteUI*> (*t);
167
168                 if (!rui) {
169                         /* some other type of timeaxis view, not a route */
170                         continue;
171                 }
172
173                 if (rui->route()->is_master() || rui->route()->is_monitor()) {
174                         /* no option to duplicate these */
175                         continue;
176                 }
177
178                 XMLNode& state (rui->route()->get_state());
179                 RouteList rl = _session->new_route_from_template (cnt, ARDOUR_UI::instance()->translate_order (insert_at()), state, std::string(), playlist_action);
180
181                 /* normally the state node would be added to a parent, and
182                  * ownership would transfer. Because we don't do that here,
183                  * we need to delete the node ourselves.
184                  */
185
186                 delete &state;
187
188                 if (rl.empty()) {
189                         err++;
190                         break;
191                 }
192         }
193
194         if (err) {
195                 MessageDialog msg (_("1 or more tracks/busses could not be duplicated"),
196                                      true, MESSAGE_ERROR, BUTTONS_OK, true);
197                 msg.set_position (WIN_POS_MOUSE);
198                 msg.run ();
199         }
200 }
201
202 RouteDialogs::InsertAt
203 DuplicateRouteDialog::insert_at ()
204 {
205         using namespace RouteDialogs;
206
207         std::string str = insert_at_combo.get_active_text();
208
209         if (str == _("First")) {
210                 return First;
211         } else if (str == _("After Selection")) {
212                 return AfterSelection;
213         } else if (str == _("Before Selection")){
214                 return BeforeSelection;
215         }
216         return Last;
217 }