Provide a function to fill a Gtk::ComboBox with all available actions
authorJohannes Mueller <github@johannes-mueller.org>
Sat, 20 Apr 2019 12:12:24 +0000 (14:12 +0200)
committerJohannes Mueller <github@johannes-mueller.org>
Sat, 20 Apr 2019 13:25:32 +0000 (15:25 +0200)
libs/gtkmm2ext/action_model.cc
libs/gtkmm2ext/gtkmm2ext/action_model.h

index cc3b62d9a6ec4baef4d70336c7ed280f369146d7..a909a8a8374423b81a84edb555c37279b0fd5466 100644 (file)
@@ -20,6 +20,8 @@
 
 #include <vector>
 
+#include  <gtkmm/combobox.h>
+
 #include "pbd/i18n.h"
 #include "pbd/strsplit.h"
 
@@ -116,3 +118,39 @@ ActionManager::ActionModel::ActionModel ()
                row[_columns.path] = *p;
        }
 }
+
+bool
+ActionManager::ActionModel::find_action_in_model (const TreeModel::iterator& iter, std::string const & action_path, TreeModel::iterator* found) const
+{
+       TreeModel::Row row = *iter;
+       string path = row[_columns.path];
+
+       if (path == action_path) {
+               *found = iter;
+               return true;
+       }
+
+       return false;
+}
+
+void
+ActionManager::ActionModel::build_action_combo (ComboBox& cb, string const& current_action) const
+{
+       cb.set_model (_model);
+       cb.pack_start (_columns.name);
+
+       if (current_action.empty()) {
+               cb.set_active (0); /* "disabled" */
+               return;
+       }
+
+       TreeModel::iterator iter = _model->children().end();
+
+       _model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &ActionManager::ActionModel::find_action_in_model), current_action, &iter));
+
+       if (iter != _model->children().end()) {
+               cb.set_active (iter);
+       } else {
+               cb.set_active (0);
+       }
+}
index b269499ecc727ca0d706fb7a3677cf14db54e354..4716660b144dae2c8fff8f0b1587152d3c2016b0 100644 (file)
 
 #include "gtkmm2ext/visibility.h"
 
+namespace Gtk
+{
+class ComboBox;
+}
+
 /*
   The singleton ActionModel provides a Gtk::Treestore of all actions known to
   ardour.
@@ -55,9 +60,13 @@ public:
 
        const Columns& columns() const { return _columns; }
 
+       void build_action_combo (Gtk::ComboBox& cb, std::string const& current_action) const;
+
 private:
        ActionModel ();
 
+       bool find_action_in_model (const Gtk::TreeModel::iterator& iter, std::string const & action_path, Gtk::TreeModel::iterator* found) const;
+
        const Columns _columns;
        Glib::RefPtr<Gtk::TreeStore> _model;
 };