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