6db5b158a145b77a8d7ca9afe5f416e2f36358ca
[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 "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);
45         get_vbox()->pack_start (count_box, false, false, 20);
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         get_vbox()->show_all ();
53
54         add_button (Stock::CANCEL, RESPONSE_CANCEL);
55         add_button (Stock::OK, RESPONSE_OK);
56 }
57
58 int
59 DuplicateRouteDialog::restart ()
60 {
61         TrackSelection& tracks  (PublicEditor::instance().get_selection().tracks);
62         uint32_t ntracks = 0;
63         uint32_t nbusses = 0;
64
65         for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
66
67                 RouteUI* rui = dynamic_cast<RouteUI*> (*t);
68
69                 if (!rui) {
70                         /* some other type of timeaxis view, not a route */
71                         continue;
72                 }
73
74                 boost::shared_ptr<Route> r (rui->route());
75
76                 if (boost::dynamic_pointer_cast<Track> (r)) {
77                         ntracks++;
78                 } else {
79                         if (!r->is_master() && !r->is_monitor()) {
80                                 nbusses++;
81                         }
82                 }
83         }
84
85         if (ntracks == 0 && nbusses == 0) {
86                 std::cerr << "You can't do this\n";
87                 return -1;
88         }
89
90         /* XXX grrr. Gtk Boxes do not shrink when children are removed,
91            which is what we really want to happen here.
92         */
93
94         if (ntracks == 0) {
95                 get_vbox()->remove (playlist_button_box);
96         } else {
97                 get_vbox()->pack_end (playlist_button_box, false, false);
98         }
99
100         return 0;
101 }
102
103 uint32_t
104 DuplicateRouteDialog::count() const
105 {
106         return count_adjustment.get_value ();
107 }
108
109 ARDOUR::PlaylistDisposition
110 DuplicateRouteDialog::playlist_disposition() const
111 {
112         if (new_playlists_button.get_active()) {
113                 return ARDOUR::NewPlaylist;
114         } else if (copy_playlists_button.get_active()) {
115                 return ARDOUR::CopyPlaylist;
116         }
117
118         return ARDOUR::SharePlaylist;
119 }
120
121 void
122 DuplicateRouteDialog::on_response (int response)
123 {
124         hide ();
125
126         if (response != RESPONSE_OK) {
127                 return;
128         }
129
130         ARDOUR::PlaylistDisposition playlist_action = playlist_disposition ();
131         uint32_t cnt = count ();
132
133         /* Copy the track selection because it will/may change as we add new ones */
134         TrackSelection tracks  (PublicEditor::instance().get_selection().tracks);
135         int err = 0;
136
137         for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
138
139                 RouteUI* rui = dynamic_cast<RouteUI*> (*t);
140
141                 if (!rui) {
142                         /* some other type of timeaxis view, not a route */
143                         continue;
144                 }
145
146                 if (rui->route()->is_master() || rui->route()->is_monitor()) {
147                         /* no option to duplicate these */
148                         continue;
149                 }
150
151                 XMLNode& state (rui->route()->get_state());
152                 RouteList rl = _session->new_route_from_template (cnt, state, std::string(), playlist_action);
153
154                 /* normally the state node would be added to a parent, and
155                  * ownership would transfer. Because we don't do that here,
156                  * we need to delete the node ourselves.
157                  */
158
159                 delete &state;
160
161                 if (rl.empty()) {
162                         err++;
163                         break;
164                 }
165         }
166
167         if (err) {
168                 MessageDialog msg (_("1 or more tracks/busses could not be duplicated"),
169                                      true, MESSAGE_ERROR, BUTTONS_OK, true);
170                 msg.set_position (WIN_POS_MOUSE);
171                 msg.run ();
172         }
173 }