91b06e3df11cf7dc6218ab8a497e988953753423
[ardour.git] / gtk2_ardour / bundle_manager.cc
1 /*
2     Copyright (C) 2007 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 #include <gtkmm/button.h>
22 #include <gtkmm/label.h>
23 #include <gtkmm/entry.h>
24 #include <gtkmm/table.h>
25 #include <gtkmm/comboboxtext.h>
26 #include <gtkmm/alignment.h>
27 #include "ardour/session.h"
28 #include "ardour/user_bundle.h"
29 #include "ardour/audioengine.h"
30 #include "bundle_manager.h"
31 #include "i18n.h"
32
33 BundleEditorMatrix::BundleEditorMatrix (
34         ARDOUR::Session& session, boost::shared_ptr<ARDOUR::Bundle> bundle
35         )
36         : PortMatrix (session, bundle->type()),
37           _bundle (bundle)
38 {
39         _port_group = boost::shared_ptr<PortGroup> (new PortGroup (""));
40         _port_group->add_bundle (_bundle);
41 }
42
43 void
44 BundleEditorMatrix::setup_ports (int dim)
45 {
46         if (dim == OURS) {
47                 _ports[OURS].clear ();
48                 _ports[OURS].add_group (_port_group);
49         } else {
50                 _ports[OTHER].suspend_signals ();
51                 _ports[OTHER].gather (_session, _bundle->ports_are_inputs());
52                 _ports[OTHER].remove_bundle (_bundle);
53                 _ports[OTHER].resume_signals ();
54         }
55 }
56
57 void
58 BundleEditorMatrix::set_state (ARDOUR::BundleChannel c[2], bool s)
59 {
60         ARDOUR::Bundle::PortList const& pl = c[OTHER].bundle->channel_ports (c[OTHER].channel);
61         for (ARDOUR::Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
62                 if (s) {
63                         c[OURS].bundle->add_port_to_channel (c[OURS].channel, *i);
64                 } else {
65                         c[OURS].bundle->remove_port_from_channel (c[OURS].channel, *i);
66                 }
67         }
68 }
69
70 PortMatrix::State
71 BundleEditorMatrix::get_state (ARDOUR::BundleChannel c[2]) const
72 {
73         ARDOUR::Bundle::PortList const& pl = c[OTHER].bundle->channel_ports (c[OTHER].channel);
74         for (ARDOUR::Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
75                 if (!c[OURS].bundle->port_attached_to_channel (c[OURS].channel, *i)) {
76                         return NOT_ASSOCIATED;
77                 }
78         }
79
80         return ASSOCIATED;
81 }
82
83 void
84 BundleEditorMatrix::add_channel (boost::shared_ptr<ARDOUR::Bundle> b)
85 {
86         NameChannelDialog d;
87         d.set_position (Gtk::WIN_POS_MOUSE);
88
89         if (d.run () != Gtk::RESPONSE_ACCEPT) {
90                 return;
91         }
92
93         _bundle->add_channel (d.get_name());
94         setup_ports (OURS);
95 }
96
97 void
98 BundleEditorMatrix::remove_channel (ARDOUR::BundleChannel bc)
99 {
100         bc.bundle->remove_channel (bc.channel);
101         setup_ports (OURS);
102 }
103
104 void
105 BundleEditorMatrix::rename_channel (ARDOUR::BundleChannel bc)
106 {
107         NameChannelDialog d (bc.bundle, bc.channel);
108         d.set_position (Gtk::WIN_POS_MOUSE);
109
110         if (d.run () != Gtk::RESPONSE_ACCEPT) {
111                 return;
112         }
113
114         bc.bundle->set_channel_name (bc.channel, d.get_name ());
115 }
116
117 bool
118 BundleEditorMatrix::list_is_global (int dim) const
119 {
120         return (dim == OTHER);
121 }
122
123 BundleEditor::BundleEditor (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::UserBundle> bundle, bool add)
124         : ArdourDialog (_("Edit Bundle")), _matrix (session, bundle), _bundle (bundle)
125 {
126         Gtk::Table* t = new Gtk::Table (3, 2);
127         t->set_spacings (4);
128
129         /* Bundle name */
130         Gtk::Alignment* a = new Gtk::Alignment (1, 0.5, 0, 1);
131         a->add (*Gtk::manage (new Gtk::Label (_("Name:"))));
132         t->attach (*Gtk::manage (a), 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
133         t->attach (_name, 1, 2, 0, 1);
134         _name.set_text (_bundle->name ());
135         _name.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::name_changed));
136
137         /* Direction (input or output) */
138         a = new Gtk::Alignment (1, 0.5, 0, 1);
139         a->add (*Gtk::manage (new Gtk::Label (_("Direction:"))));
140         t->attach (*Gtk::manage (a), 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
141         a = new Gtk::Alignment (0, 0.5, 0, 1);
142         a->add (_input_or_output);
143         t->attach (*Gtk::manage (a), 1, 2, 1, 2);
144         _input_or_output.append_text (_("Input"));
145         _input_or_output.append_text (_("Output"));
146         
147         if (bundle->ports_are_inputs()) {
148                 _input_or_output.set_active_text (_("Input"));
149         } else {
150                 _input_or_output.set_active_text (_("Output"));
151         }
152
153         _input_or_output.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::input_or_output_changed));
154
155         /* Type (audio or MIDI) */
156         a = new Gtk::Alignment (1, 0.5, 0, 1);
157         a->add (*Gtk::manage (new Gtk::Label (_("Type:"))));
158         t->attach (*Gtk::manage (a), 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
159         a = new Gtk::Alignment (0, 0.5, 0, 1);
160         a->add (_type);
161         t->attach (*Gtk::manage (a), 1, 2, 2, 3);
162         
163         _type.append_text (_("Audio"));
164         _type.append_text (_("MIDI"));
165         
166         switch (bundle->type ()) {
167         case ARDOUR::DataType::AUDIO:
168                 _type.set_active_text (_("Audio"));
169                 break;
170         case ARDOUR::DataType::MIDI:
171                 _type.set_active_text (_("MIDI"));
172                 break;
173         }
174
175         _type.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::type_changed));
176                                         
177         get_vbox()->pack_start (*Gtk::manage (t), false, false);
178         get_vbox()->pack_start (_matrix);
179         get_vbox()->set_spacing (4);
180
181         /* Add Channel button */
182         Gtk::Button* add_channel_button = Gtk::manage (new Gtk::Button (_("Add Channel")));
183         add_channel_button->set_name ("IOSelectorButton");
184         add_channel_button->set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::ADD, Gtk::ICON_SIZE_BUTTON)));
185         get_action_area()->pack_start (*add_channel_button, false, false);
186         add_channel_button->signal_clicked().connect (sigc::bind (sigc::mem_fun (_matrix, &BundleEditorMatrix::add_channel), boost::shared_ptr<ARDOUR::Bundle> ()));
187
188         if (add) {
189                 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
190                 add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
191         } else {
192                 add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_ACCEPT);
193         }
194
195         show_all ();
196 }
197
198 void
199 BundleEditor::name_changed ()
200 {
201         _bundle->set_name (_name.get_text ());
202 }
203
204 void
205 BundleEditor::input_or_output_changed ()
206 {
207         _bundle->remove_ports_from_channels ();
208         
209         if (_input_or_output.get_active_text() == _("Output")) {
210                 _bundle->set_ports_are_outputs ();
211         } else {
212                 _bundle->set_ports_are_inputs ();
213         }
214
215         _matrix.setup_all_ports ();
216 }
217
218 void
219 BundleEditor::type_changed ()
220 {
221         _bundle->remove_ports_from_channels ();
222         
223         ARDOUR::DataType const t = _type.get_active_text() == _("Audio") ?
224                 ARDOUR::DataType::AUDIO : ARDOUR::DataType::MIDI;
225
226         _bundle->set_type (t);
227         _matrix.set_type (t);
228 }
229
230 void
231 BundleEditor::on_map ()
232 {
233         _matrix.setup_all_ports ();
234         Window::on_map ();
235 }
236
237
238 BundleManager::BundleManager (ARDOUR::Session& session)
239         : ArdourDialog (_("Bundle manager")), _session (session), edit_button (_("Edit")), delete_button (_("Delete"))
240 {
241         _list_model = Gtk::ListStore::create (_list_model_columns);
242         _tree_view.set_model (_list_model);
243         _tree_view.append_column (_("Name"), _list_model_columns.name);
244         _tree_view.set_headers_visible (false);
245
246         boost::shared_ptr<ARDOUR::BundleList> bundles = _session.bundles ();
247         for (ARDOUR::BundleList::iterator i = bundles->begin(); i != bundles->end(); ++i) {
248                 add_bundle (*i);
249         }
250         
251         /* New / Edit / Delete buttons */
252         Gtk::VBox* buttons = new Gtk::VBox;
253         buttons->set_spacing (8);
254         Gtk::Button* b = new Gtk::Button (_("New"));
255         b->set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
256         b->signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::new_clicked));
257         buttons->pack_start (*Gtk::manage (b), false, false);
258         edit_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::EDIT, Gtk::ICON_SIZE_BUTTON)));
259         edit_button.signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::edit_clicked));
260         buttons->pack_start (edit_button, false, false);
261         delete_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::DELETE, Gtk::ICON_SIZE_BUTTON)));
262         delete_button.signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::delete_clicked));
263         buttons->pack_start (delete_button, false, false);
264         
265         Gtk::HBox* h = new Gtk::HBox;
266         h->set_spacing (8);
267         h->set_border_width (8);
268         h->pack_start (_tree_view);
269         h->pack_start (*Gtk::manage (buttons), false, false);
270
271         get_vbox()->set_spacing (8);
272         get_vbox()->pack_start (*Gtk::manage (h));
273
274         set_default_size (480, 240);
275
276         _tree_view.get_selection()->signal_changed().connect (
277                 sigc::mem_fun (*this, &BundleManager::set_button_sensitivity)
278                 );
279
280         set_button_sensitivity ();
281
282         show_all ();
283 }
284
285 void
286 BundleManager::set_button_sensitivity ()
287 {
288         bool const sel = (_tree_view.get_selection()->get_selected() != 0);
289         edit_button.set_sensitive (sel);
290         delete_button.set_sensitive (sel);
291 }
292
293
294 void
295 BundleManager::new_clicked ()
296 {
297         boost::shared_ptr<ARDOUR::UserBundle> b (new ARDOUR::UserBundle (""));
298
299         /* Start off with a single channel */
300         b->add_channel ("");
301
302         BundleEditor e (_session, b, true);
303
304         if (e.run () == Gtk::RESPONSE_ACCEPT) {
305                 _session.add_bundle (b);
306                 add_bundle (b);
307         }
308 }
309
310 void
311 BundleManager::edit_clicked ()
312 {
313         Gtk::TreeModel::iterator i = _tree_view.get_selection()->get_selected();
314         if (i) {
315                 boost::shared_ptr<ARDOUR::UserBundle> b = (*i)[_list_model_columns.bundle];
316                 BundleEditor e (_session, b, false);
317                 if (e.run () == Gtk::RESPONSE_ACCEPT) {
318                         _session.set_dirty ();
319                 }
320         }
321 }
322
323 void
324 BundleManager::delete_clicked ()
325 {
326         Gtk::TreeModel::iterator i = _tree_view.get_selection()->get_selected();
327         if (i) {
328                 boost::shared_ptr<ARDOUR::UserBundle> b = (*i)[_list_model_columns.bundle];
329                 _session.remove_bundle (b);
330                 _list_model->erase (i);
331         }
332 }
333
334 void
335 BundleManager::add_bundle (boost::shared_ptr<ARDOUR::Bundle> b)
336 {
337         boost::shared_ptr<ARDOUR::UserBundle> u = boost::dynamic_pointer_cast<ARDOUR::UserBundle> (b);
338         if (u == 0) {
339                 return;
340         }
341
342         Gtk::TreeModel::iterator i = _list_model->append ();
343         (*i)[_list_model_columns.name] = u->name ();
344         (*i)[_list_model_columns.bundle] = u;
345
346         u->Changed.connect (sigc::bind (sigc::mem_fun (*this, &BundleManager::bundle_changed), u));
347 }
348
349 void
350 BundleManager::bundle_changed (ARDOUR::Bundle::Change c, boost::shared_ptr<ARDOUR::UserBundle> b)
351 {
352         if ((c & ARDOUR::Bundle::NameChanged) == 0) {
353                 return;
354         }
355         
356         Gtk::TreeModel::iterator i = _list_model->children().begin ();
357         while (i != _list_model->children().end()) {
358                 boost::shared_ptr<ARDOUR::UserBundle> t = (*i)[_list_model_columns.bundle];
359                 if (t == b) {
360                         break;
361                 }
362                 ++i;
363         }
364
365         if (i != _list_model->children().end()) {
366                 (*i)[_list_model_columns.name] = b->name ();
367         }
368 }
369
370
371 NameChannelDialog::NameChannelDialog ()
372         : ArdourDialog (_("Add channel")),
373           _adding (true)
374 {
375         setup ();
376 }
377
378 NameChannelDialog::NameChannelDialog (boost::shared_ptr<ARDOUR::Bundle> b, uint32_t c)
379         : ArdourDialog (_("Rename channel")),
380           _bundle (b),
381           _channel (c),
382           _adding (false)
383 {
384         _name.set_text (b->channel_name (c));
385
386         setup ();
387 }
388
389 void
390 NameChannelDialog::setup ()
391 {       
392         Gtk::HBox* box = Gtk::manage (new Gtk::HBox ());
393
394         box->pack_start (*Gtk::manage (new Gtk::Label (_("Name"))));
395         box->pack_start (_name);
396
397         get_vbox ()->pack_end (*box);
398         box->show_all ();
399
400         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
401         if (_adding) {
402                 add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
403         } else {
404                 add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_ACCEPT);
405         }
406         set_default_response (Gtk::RESPONSE_ACCEPT);
407 }
408
409 std::string
410 NameChannelDialog::get_name () const
411 {
412         return _name.get_text ();
413 }