Make Bundles work a bit better. A few include optimisations.
[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 (
37                 session, bundle->type(), bundle->ports_are_inputs(),
38                 PortGroupList::Mask (PortGroupList::SYSTEM | PortGroupList::OTHER)
39                 )
40 {
41         _bundle = boost::dynamic_pointer_cast<ARDOUR::UserBundle> (bundle);
42         assert (_bundle != 0);
43 }
44
45 void
46 BundleEditorMatrix::set_state (int r, std::string const & p, bool s, uint32_t keymod)
47 {
48         if (s) {
49                 _bundle->add_port_to_channel (r, p);
50         } else {
51                 _bundle->remove_port_from_channel (r, p);
52         }
53 }
54
55 bool
56 BundleEditorMatrix::get_state (int r, std::string const & p) const
57 {
58         return _bundle->port_attached_to_channel (r, p);
59 }
60
61 uint32_t
62 BundleEditorMatrix::n_rows () const
63 {
64         return _bundle->nchannels ();
65 }
66
67 uint32_t
68 BundleEditorMatrix::maximum_rows () const
69 {
70         /* 65536 channels in a bundle ought to be enough for anyone (TM) */
71         return 65536;
72 }
73
74 uint32_t
75 BundleEditorMatrix::minimum_rows () const
76 {
77         return 0;
78 }
79
80 std::string
81 BundleEditorMatrix::row_name (int r) const
82 {
83         std::stringstream s;
84         s << r + 1; // 1-based counting
85         return s.str();
86 }
87
88 void
89 BundleEditorMatrix::add_row ()
90 {
91         _bundle->add_channel ();
92         setup ();
93 }
94
95 void
96 BundleEditorMatrix::remove_row (int r)
97 {
98         _bundle->remove_channel (r);
99         setup ();
100 }
101
102 std::string
103 BundleEditorMatrix::row_descriptor () const
104 {
105         return _("channel");
106 }
107
108 BundleEditor::BundleEditor (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::UserBundle> bundle, bool add)
109         : ArdourDialog (_("Edit Bundle")), _matrix (session, bundle), _bundle (bundle)
110 {
111         Gtk::Table* t = new Gtk::Table (3, 2);
112         t->set_spacings (4);
113
114         Gtk::Alignment* a = new Gtk::Alignment (1, 0.5, 0, 1);
115         a->add (*Gtk::manage (new Gtk::Label (_("Name:"))));
116         t->attach (*Gtk::manage (a), 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
117         t->attach (_name, 1, 2, 0, 1);
118         
119         _name.set_text (_bundle->name ());
120         _name.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::name_changed));
121
122         a = new Gtk::Alignment (1, 0.5, 0, 1);
123         a->add (*Gtk::manage (new Gtk::Label (_("Direction:"))));
124         t->attach (*Gtk::manage (a), 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
125         a = new Gtk::Alignment (0, 0.5, 0, 1);
126         a->add (_input_or_output);
127         t->attach (*Gtk::manage (a), 1, 2, 1, 2);
128         
129         _input_or_output.append_text (_("Input"));
130         _input_or_output.append_text (_("Output"));
131         
132         if (bundle->ports_are_inputs()) {
133                 _input_or_output.set_active_text (_("Output"));
134         } else {
135                 _input_or_output.set_active_text (_("Input"));
136         }
137
138         _input_or_output.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::input_or_output_changed));
139
140         a = new Gtk::Alignment (1, 0.5, 0, 1);
141         a->add (*Gtk::manage (new Gtk::Label (_("Type:"))));
142         t->attach (*Gtk::manage (a), 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
143         a = new Gtk::Alignment (0, 0.5, 0, 1);
144         a->add (_type);
145         t->attach (*Gtk::manage (a), 1, 2, 2, 3);
146         
147         _type.append_text (_("Audio"));
148         _type.append_text (_("MIDI"));
149         
150         switch (bundle->type ()) {
151         case ARDOUR::DataType::AUDIO:
152                 _type.set_active_text (_("Audio"));
153                 break;
154         case ARDOUR::DataType::MIDI:
155                 _type.set_active_text (_("MIDI"));
156                 break;
157         }
158
159         _type.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::type_changed));
160                                         
161         get_vbox()->pack_start (*Gtk::manage (t), false, false);
162         
163         get_vbox()->pack_start (_matrix);
164
165         get_vbox()->set_spacing (4);
166
167         if (add) {
168                 add_button (Gtk::Stock::CANCEL, 1);
169                 add_button (Gtk::Stock::ADD, 0);
170         } else {
171                 add_button (Gtk::Stock::CLOSE, 0);
172         }
173
174         show_all ();
175 }
176
177 void
178 BundleEditor::name_changed ()
179 {
180         _bundle->set_name (_name.get_text ());
181 }
182
183 void
184 BundleEditor::input_or_output_changed ()
185 {
186         if (_input_or_output.get_active_text() == _("Output")) {
187                 _bundle->set_ports_are_inputs ();
188                 _matrix.set_offer_inputs (true);
189         } else {
190                 _bundle->set_ports_are_outputs ();
191                 _matrix.set_offer_inputs (false);
192         }
193 }
194
195 void
196 BundleEditor::type_changed ()
197 {
198         ARDOUR::DataType const t = _type.get_active_text() == _("Audio") ?
199                 ARDOUR::DataType::AUDIO : ARDOUR::DataType::MIDI;
200
201         _bundle->set_type (t);
202         _matrix.set_type (t);
203 }
204
205 void
206 BundleEditor::on_map ()
207 {
208         _matrix.setup ();
209         Window::on_map ();
210 }
211
212
213 BundleManager::BundleManager (ARDOUR::Session& session)
214         : ArdourDialog (_("Bundle manager")), _session (session), edit_button (_("Edit")), delete_button (_("Delete"))
215 {
216         _list_model = Gtk::ListStore::create (_list_model_columns);
217         _tree_view.set_model (_list_model);
218         _tree_view.append_column (_("Name"), _list_model_columns.name);
219         _tree_view.set_headers_visible (false);
220
221         _session.foreach_bundle (sigc::mem_fun (*this, &BundleManager::add_bundle));
222         
223         /* New / Edit / Delete buttons */
224         Gtk::VBox* buttons = new Gtk::VBox;
225         buttons->set_spacing (8);
226         Gtk::Button* b = new Gtk::Button (_("New"));
227         b->set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
228         b->signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::new_clicked));
229         buttons->pack_start (*Gtk::manage (b), false, false);
230         edit_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::EDIT, Gtk::ICON_SIZE_BUTTON)));
231         edit_button.signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::edit_clicked));
232         buttons->pack_start (edit_button, false, false);
233         delete_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::DELETE, Gtk::ICON_SIZE_BUTTON)));
234         delete_button.signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::delete_clicked));
235         buttons->pack_start (delete_button, false, false);
236         
237         Gtk::HBox* h = new Gtk::HBox;
238         h->set_spacing (8);
239         h->set_border_width (8);
240         h->pack_start (_tree_view);
241         h->pack_start (*Gtk::manage (buttons), false, false);
242
243         get_vbox()->set_spacing (8);
244         get_vbox()->pack_start (*Gtk::manage (h));
245
246         set_default_size (480, 240);
247
248         _tree_view.get_selection()->signal_changed().connect (
249                 sigc::mem_fun (*this, &BundleManager::set_button_sensitivity)
250                 );
251
252         set_button_sensitivity ();
253
254         show_all ();
255 }
256
257 void
258 BundleManager::set_button_sensitivity ()
259 {
260         bool const sel = (_tree_view.get_selection()->get_selected() != 0);
261         edit_button.set_sensitive (sel);
262         delete_button.set_sensitive (sel);
263 }
264
265
266 void
267 BundleManager::new_clicked ()
268 {
269         boost::shared_ptr<ARDOUR::UserBundle> b (new ARDOUR::UserBundle (""));
270
271         /* Start off with a single channel */
272         b->add_channel ();
273
274         BundleEditor e (_session, b, true);
275         if (e.run () == 0) {
276                 _session.add_bundle (b);
277                 add_bundle (b);
278         }
279 }
280
281 void
282 BundleManager::edit_clicked ()
283 {
284         Gtk::TreeModel::iterator i = _tree_view.get_selection()->get_selected();
285         if (i) {
286                 boost::shared_ptr<ARDOUR::UserBundle> b = (*i)[_list_model_columns.bundle];
287                 BundleEditor e (_session, b, false);
288                 e.run ();
289         }
290         
291 }
292
293 void
294 BundleManager::delete_clicked ()
295 {
296         Gtk::TreeModel::iterator i = _tree_view.get_selection()->get_selected();
297         if (i) {
298                 boost::shared_ptr<ARDOUR::UserBundle> b = (*i)[_list_model_columns.bundle];
299                 _session.remove_bundle (b);
300                 _list_model->erase (i);
301         }
302 }
303
304 void
305 BundleManager::add_bundle (boost::shared_ptr<ARDOUR::Bundle> b)
306 {
307         boost::shared_ptr<ARDOUR::UserBundle> u = boost::dynamic_pointer_cast<ARDOUR::UserBundle> (b);
308         if (u == 0) {
309                 return;
310         }
311
312         Gtk::TreeModel::iterator i = _list_model->append ();
313         (*i)[_list_model_columns.name] = u->name ();
314         (*i)[_list_model_columns.bundle] = u;
315
316         u->NameChanged.connect (sigc::bind (sigc::mem_fun (*this, &BundleManager::bundle_name_changed), u));
317 }
318
319 void
320 BundleManager::bundle_name_changed (boost::shared_ptr<ARDOUR::UserBundle> b)
321 {
322         Gtk::TreeModel::iterator i = _list_model->children().begin ();
323         while (i != _list_model->children().end()) {
324                 boost::shared_ptr<ARDOUR::UserBundle> t = (*i)[_list_model_columns.bundle];
325                 if (t == b) {
326                         break;
327                 }
328                 ++i;
329         }
330
331         if (i != _list_model->children().end()) {
332                 (*i)[_list_model_columns.name] = b->name ();
333         }
334 }
335