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