Fix ExportFormatSpecification copy-c'tor
[ardour.git] / gtk2_ardour / visibility_group.cc
index c817ff3fcb1dba86429d0c98ea075bc8d24fdcc9..d0ef1dd63510a0c36b9253a4421ab724410b2d06 100644 (file)
 #include <gtkmm/menu.h>
 #include <gtkmm/menushell.h>
 #include <gtkmm/treeview.h>
+
+#include "pbd/strsplit.h"
 #include "pbd/xml++.h"
+
 #include "visibility_group.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 
@@ -40,17 +43,21 @@ VisibilityGroup::VisibilityGroup (std::string const & name)
  *  @param id Some single-word ID to be used for the state of this member in XML.
  *  @param name User-visible name for the widget.
  *  @param visible true to default to visible, otherwise false.
+ *  @param override A functor to decide whether the visibility specified by the member should be
+ *  overridden by some external factor; if the returned optional value is given, it will be used
+ *  to override whatever visibility setting the member has.
  */
 
 void
-VisibilityGroup::add (Gtk::Widget* widget, string const & id, string const & name, bool visible)
+VisibilityGroup::add (Gtk::Widget* widget, string const & id, string const & name, bool visible, boost::function<boost::optional<bool> ()> override)
 {
        Member m;
        m.widget = widget;
        m.id = id;
        m.name = name;
        m.visible = visible;
-       
+       m.override = override;
+
        _members.push_back (m);
 }
 
@@ -84,13 +91,27 @@ VisibilityGroup::menu ()
        return m;
 }
 
+/** @return true if the member should be visible, even taking into account any override functor */
+bool
+VisibilityGroup::should_actually_be_visible (Member const & m) const
+{
+       if (m.override) {
+               boost::optional<bool> o = m.override ();
+               if (o) {
+                       return o.get ();
+               }
+       }
+
+       return m.visible;
+}
+
 /** Update visible consequences of any changes to our _members vector */
 void
 VisibilityGroup::update ()
 {
        for (vector<Member>::iterator i = _members.begin(); i != _members.end(); ++i) {
                if (i->widget) {
-                       if (i->visible) {
+                       if (should_actually_be_visible (*i)) {
                                i->widget->show ();
                        } else {
                                i->widget->hide ();
@@ -143,12 +164,58 @@ VisibilityGroup::set_state (string v)
                }
 
                v = v.substr (comma + 1);
-               
+
        } while (1);
 
        update ();
 }
 
+string
+VisibilityGroup::remove_element (std::string const& from, std::string const& element)
+{
+       std::vector<string> s;
+       std::string ret;
+
+       split (from, s, ',');
+       for (std::vector<string>::const_iterator i = s.begin(); i != s.end(); ++i) {
+               if ((*i) == element) {
+                       continue;
+               }
+               if (!ret.empty()) {
+                       ret += ',';
+               }
+               ret += *i;
+       }
+
+       return ret;
+}
+
+string
+VisibilityGroup::add_element (std::string const& from, std::string const& element)
+{
+       std::vector<string> s;
+       std::string ret;
+       
+       split (from, s, ',');
+
+       for (std::vector<string>::const_iterator i = s.begin(); i != s.end(); ++i) {
+               if ((*i) == element) {
+                       /* already present, just return the original */
+                       return from;
+               }
+       }
+
+       ret = from;
+
+       if (!ret.empty()) {
+               ret += ',';
+       }
+
+       ret += element;
+
+       return ret;
+}
+
 string
 VisibilityGroup::get_state_name () const
 {
@@ -177,11 +244,11 @@ VisibilityGroup::update_list_view ()
        if (!_model) {
                return;
        }
-       
+
        _ignore_list_view_change = true;
 
        _model->clear ();
-       
+
        for (vector<Member>::iterator i = _members.begin(); i != _members.end(); ++i) {
                Gtk::TreeModel::iterator j = _model->append ();
                Gtk::TreeModel::Row row = *j;
@@ -202,8 +269,8 @@ VisibilityGroup::list_view ()
 
        Gtk::TreeView* v = Gtk::manage (new Gtk::TreeView (_model));
        v->set_headers_visible (false);
-       v->append_column (_(""), _model_columns._visible);
-       v->append_column (_(""), _model_columns._name);
+       v->append_column ("", _model_columns._visible);
+       v->append_column ("", _model_columns._name);
 
        Gtk::CellRendererToggle* visible_cell = dynamic_cast<Gtk::CellRendererToggle*> (v->get_column_cell_renderer (0));
        visible_cell->property_activatable() = true;
@@ -217,7 +284,7 @@ VisibilityGroup::list_view_visible_changed (string const & path)
        if (_ignore_list_view_change) {
                return;
        }
-       
+
        Gtk::TreeModel::iterator i = _model->get_iter (path);
        if (!i) {
                return;