Remove idiocy.
[ardour2.git] / gtk2_ardour / bundle_manager.cc
blobff7cec26338000783bab9e7b5d2033e55643be2e
1 /*
2 Copyright (C) 2007 Paul Davis
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.
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.
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.
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 #include "utils.h"
34 using namespace std;
35 using namespace ARDOUR;
37 BundleEditorMatrix::BundleEditorMatrix (
38 Gtk::Window* parent, Session& session, boost::shared_ptr<Bundle> bundle
40 : PortMatrix (parent, session, bundle->type()),
41 _bundle (bundle)
43 _port_group = boost::shared_ptr<PortGroup> (new PortGroup (""));
44 _port_group->add_bundle (_bundle);
47 void
48 BundleEditorMatrix::setup_ports (int dim)
50 if (dim == OURS) {
51 _ports[OURS].clear ();
52 _ports[OURS].add_group (_port_group);
53 } else {
54 _ports[OTHER].suspend_signals ();
55 _ports[OTHER].gather (_session, _bundle->ports_are_inputs());
56 _ports[OTHER].remove_bundle (_bundle);
57 _ports[OTHER].resume_signals ();
61 void
62 BundleEditorMatrix::set_state (BundleChannel c[2], bool s)
64 Bundle::PortList const& pl = c[OTHER].bundle->channel_ports (c[OTHER].channel);
65 for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
66 if (s) {
67 c[OURS].bundle->add_port_to_channel (c[OURS].channel, *i);
68 } else {
69 c[OURS].bundle->remove_port_from_channel (c[OURS].channel, *i);
74 PortMatrixNode::State
75 BundleEditorMatrix::get_state (BundleChannel c[2]) const
77 Bundle::PortList const& pl = c[OTHER].bundle->channel_ports (c[OTHER].channel);
78 for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
79 if (!c[OURS].bundle->port_attached_to_channel (c[OURS].channel, *i)) {
80 return PortMatrixNode::NOT_ASSOCIATED;
84 return PortMatrixNode::ASSOCIATED;
87 bool
88 BundleEditorMatrix::can_add_channel (boost::shared_ptr<Bundle> b) const
90 if (b == _bundle) {
91 return true;
94 return PortMatrix::can_add_channel (b);
97 void
98 BundleEditorMatrix::add_channel (boost::shared_ptr<Bundle> b)
100 if (b == _bundle) {
102 NameChannelDialog d;
103 d.set_position (Gtk::WIN_POS_MOUSE);
105 if (d.run () != Gtk::RESPONSE_ACCEPT) {
106 return;
109 _bundle->add_channel (d.get_name());
110 setup_ports (OURS);
112 } else {
114 PortMatrix::add_channel (b);
119 bool
120 BundleEditorMatrix::can_remove_channels (boost::shared_ptr<Bundle> b) const
122 if (b == _bundle) {
123 return true;
126 return PortMatrix::can_remove_channels (b);
129 void
130 BundleEditorMatrix::remove_channel (BundleChannel bc)
132 bc.bundle->remove_channel (bc.channel);
133 setup_ports (OURS);
136 bool
137 BundleEditorMatrix::can_rename_channels (boost::shared_ptr<Bundle> b) const
139 if (b == _bundle) {
140 return true;
143 return PortMatrix::can_rename_channels (b);
146 void
147 BundleEditorMatrix::rename_channel (BundleChannel bc)
149 NameChannelDialog d (bc.bundle, bc.channel);
150 d.set_position (Gtk::WIN_POS_MOUSE);
152 if (d.run () != Gtk::RESPONSE_ACCEPT) {
153 return;
156 bc.bundle->set_channel_name (bc.channel, d.get_name ());
159 bool
160 BundleEditorMatrix::list_is_global (int dim) const
162 return (dim == OTHER);
165 BundleEditor::BundleEditor (Session& session, boost::shared_ptr<UserBundle> bundle, bool add)
166 : ArdourDialog (_("Edit Bundle")), _matrix (this, session, bundle), _bundle (bundle)
168 Gtk::Table* t = new Gtk::Table (3, 2);
169 t->set_spacings (4);
171 /* Bundle name */
172 Gtk::Alignment* a = new Gtk::Alignment (1, 0.5, 0, 1);
173 a->add (*Gtk::manage (new Gtk::Label (_("Name:"))));
174 t->attach (*Gtk::manage (a), 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
175 t->attach (_name, 1, 2, 0, 1);
176 _name.set_text (_bundle->name ());
177 _name.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::name_changed));
179 /* Direction (input or output) */
180 a = new Gtk::Alignment (1, 0.5, 0, 1);
181 a->add (*Gtk::manage (new Gtk::Label (_("Direction:"))));
182 t->attach (*Gtk::manage (a), 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
183 a = new Gtk::Alignment (0, 0.5, 0, 1);
184 a->add (_input_or_output);
185 t->attach (*Gtk::manage (a), 1, 2, 1, 2);
186 _input_or_output.append_text (_("Input"));
187 _input_or_output.append_text (_("Output"));
189 if (bundle->ports_are_inputs()) {
190 _input_or_output.set_active_text (_("Input"));
191 } else {
192 _input_or_output.set_active_text (_("Output"));
195 _input_or_output.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::input_or_output_changed));
197 /* Type (audio or MIDI) */
198 a = new Gtk::Alignment (1, 0.5, 0, 1);
199 a->add (*Gtk::manage (new Gtk::Label (_("Type:"))));
200 t->attach (*Gtk::manage (a), 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
201 a = new Gtk::Alignment (0, 0.5, 0, 1);
202 a->add (_type);
203 t->attach (*Gtk::manage (a), 1, 2, 2, 3);
205 _type.append_text (_("Audio"));
206 _type.append_text (_("MIDI"));
208 switch (bundle->type ()) {
209 case DataType::AUDIO:
210 _type.set_active_text (_("Audio"));
211 break;
212 case DataType::MIDI:
213 _type.set_active_text (_("MIDI"));
214 break;
217 _type.signal_changed().connect (sigc::mem_fun (*this, &BundleEditor::type_changed));
219 get_vbox()->pack_start (*Gtk::manage (t), false, false);
220 get_vbox()->pack_start (_matrix);
221 get_vbox()->set_spacing (4);
223 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
224 if (add) {
225 add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
226 } else {
227 add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_ACCEPT);
230 show_all ();
233 void
234 BundleEditor::on_show ()
236 Gtk::Window::on_show ();
237 pair<uint32_t, uint32_t> const pm_max = _matrix.max_size ();
238 resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
241 void
242 BundleEditor::name_changed ()
244 _bundle->set_name (_name.get_text ());
247 void
248 BundleEditor::input_or_output_changed ()
250 _bundle->remove_ports_from_channels ();
252 if (_input_or_output.get_active_text() == _("Output")) {
253 _bundle->set_ports_are_outputs ();
254 } else {
255 _bundle->set_ports_are_inputs ();
258 _matrix.setup_all_ports ();
261 void
262 BundleEditor::type_changed ()
264 _bundle->remove_ports_from_channels ();
266 DataType const t = _type.get_active_text() == _("Audio") ?
267 DataType::AUDIO : DataType::MIDI;
269 _bundle->set_type (t);
270 _matrix.set_type (t);
273 void
274 BundleEditor::on_map ()
276 _matrix.setup_all_ports ();
277 Window::on_map ();
281 BundleManager::BundleManager (Session& session)
282 : ArdourDialog (_("Bundle Manager")), _session (session), edit_button (_("Edit")), delete_button (_("Delete"))
284 _list_model = Gtk::ListStore::create (_list_model_columns);
285 _tree_view.set_model (_list_model);
286 _tree_view.append_column (_("Name"), _list_model_columns.name);
287 _tree_view.set_headers_visible (false);
289 boost::shared_ptr<BundleList> bundles = _session.bundles ();
290 for (BundleList::iterator i = bundles->begin(); i != bundles->end(); ++i) {
291 add_bundle (*i);
294 /* New / Edit / Delete buttons */
295 Gtk::VBox* buttons = new Gtk::VBox;
296 buttons->set_spacing (8);
297 Gtk::Button* b = new Gtk::Button (_("New"));
298 b->set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::NEW, Gtk::ICON_SIZE_BUTTON)));
299 b->signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::new_clicked));
300 buttons->pack_start (*Gtk::manage (b), false, false);
301 edit_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::EDIT, Gtk::ICON_SIZE_BUTTON)));
302 edit_button.signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::edit_clicked));
303 buttons->pack_start (edit_button, false, false);
304 delete_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::DELETE, Gtk::ICON_SIZE_BUTTON)));
305 delete_button.signal_clicked().connect (sigc::mem_fun (*this, &BundleManager::delete_clicked));
306 buttons->pack_start (delete_button, false, false);
308 Gtk::HBox* h = new Gtk::HBox;
309 h->set_spacing (8);
310 h->set_border_width (8);
311 h->pack_start (_tree_view);
312 h->pack_start (*Gtk::manage (buttons), false, false);
314 get_vbox()->set_spacing (8);
315 get_vbox()->pack_start (*Gtk::manage (h));
317 set_default_size (480, 240);
319 _tree_view.get_selection()->signal_changed().connect (
320 sigc::mem_fun (*this, &BundleManager::set_button_sensitivity)
323 set_button_sensitivity ();
325 show_all ();
328 void
329 BundleManager::set_button_sensitivity ()
331 bool const sel = (_tree_view.get_selection()->get_selected() != 0);
332 edit_button.set_sensitive (sel);
333 delete_button.set_sensitive (sel);
337 void
338 BundleManager::new_clicked ()
340 boost::shared_ptr<UserBundle> b (new UserBundle (_("Bundle")));
342 /* Start off with a single channel */
343 b->add_channel ("1");
345 BundleEditor e (_session, b, true);
347 if (e.run () == Gtk::RESPONSE_ACCEPT) {
348 _session.add_bundle (b);
349 add_bundle (b);
353 void
354 BundleManager::edit_clicked ()
356 Gtk::TreeModel::iterator i = _tree_view.get_selection()->get_selected();
357 if (i) {
358 boost::shared_ptr<UserBundle> b = (*i)[_list_model_columns.bundle];
359 BundleEditor e (_session, b, false);
360 if (e.run () == Gtk::RESPONSE_ACCEPT) {
361 _session.set_dirty ();
366 void
367 BundleManager::delete_clicked ()
369 Gtk::TreeModel::iterator i = _tree_view.get_selection()->get_selected();
370 if (i) {
371 boost::shared_ptr<UserBundle> b = (*i)[_list_model_columns.bundle];
372 _session.remove_bundle (b);
373 _list_model->erase (i);
377 void
378 BundleManager::add_bundle (boost::shared_ptr<Bundle> b)
380 boost::shared_ptr<UserBundle> u = boost::dynamic_pointer_cast<UserBundle> (b);
381 if (u == 0) {
382 return;
385 Gtk::TreeModel::iterator i = _list_model->append ();
386 (*i)[_list_model_columns.name] = u->name ();
387 (*i)[_list_model_columns.bundle] = u;
389 u->Changed.connect (sigc::bind (sigc::mem_fun (*this, &BundleManager::bundle_changed), u));
392 void
393 BundleManager::bundle_changed (Bundle::Change c, boost::shared_ptr<UserBundle> b)
395 if ((c & Bundle::NameChanged) == 0) {
396 return;
399 Gtk::TreeModel::iterator i = _list_model->children().begin ();
400 while (i != _list_model->children().end()) {
401 boost::shared_ptr<UserBundle> t = (*i)[_list_model_columns.bundle];
402 if (t == b) {
403 break;
405 ++i;
408 if (i != _list_model->children().end()) {
409 (*i)[_list_model_columns.name] = b->name ();
413 NameChannelDialog::NameChannelDialog ()
414 : ArdourDialog (_("Add channel")),
415 _adding (true)
417 setup ();
420 NameChannelDialog::NameChannelDialog (boost::shared_ptr<Bundle> b, uint32_t c)
421 : ArdourDialog (_("Rename channel")),
422 _bundle (b),
423 _channel (c),
424 _adding (false)
426 _name.set_text (b->channel_name (c));
428 setup ();
431 void
432 NameChannelDialog::setup ()
434 Gtk::HBox* box = Gtk::manage (new Gtk::HBox ());
436 box->pack_start (*Gtk::manage (new Gtk::Label (_("Name"))));
437 box->pack_start (_name);
439 get_vbox ()->pack_end (*box);
440 box->show_all ();
442 add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
443 if (_adding) {
444 add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
445 } else {
446 add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_ACCEPT);
448 set_default_response (Gtk::RESPONSE_ACCEPT);
451 string
452 NameChannelDialog::get_name () const
454 return _name.get_text ();