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