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