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