Allow strips to add or remove personal sends
[ardour.git] / gtk2_ardour / template_dialog.cc
index 745e1ab645575b3f4283fae5e5d47a29c6ea1de9..e250791b62b6550e9c0861e632d740bcdfafd832 100644 (file)
@@ -45,6 +45,7 @@
 
 #include "gtkmm2ext/gui_thread.h"
 
+#include "ardour/filename_extensions.h"
 #include "ardour/filesystem_paths.h"
 #include "ardour/template_utils.h"
 
@@ -63,7 +64,7 @@ class TemplateManager : public Gtk::HBox,
 public:
        virtual ~TemplateManager () {}
 
-       void init ();
+       virtual void init () = 0;
        void handle_dirty_description ();
 
        PBD::Signal0<void> TemplatesImported;
@@ -71,6 +72,9 @@ public:
 protected:
        TemplateManager ();
 
+       Gtk::TextView _description_editor;
+       Gtk::Button _save_desc;
+
        void setup_model (const std::vector<ARDOUR::TemplateInfo>& templates);
 
        void row_selection_changed ();
@@ -125,8 +129,6 @@ private:
        Gtk::CellRendererText _validating_cellrenderer;
        Gtk::TreeView::Column _validated_column;
 
-       Gtk::TextView _description_editor;
-       Gtk::Button _save_desc;
        bool _desc_dirty;
 
        Gtk::Button _remove_button;
@@ -135,6 +137,8 @@ private:
        Gtk::Button _export_all_templates_button;
        Gtk::Button _import_template_set_button;
 
+       sigc::connection _cursor_changed_connection;
+
        void update_progress_gui (float p);
 };
 
@@ -144,6 +148,8 @@ public:
        SessionTemplateManager () : TemplateManager () {}
        ~SessionTemplateManager () {}
 
+       void init ();
+
        void get_templates (vector<TemplateInfo>& templates) const;
 
 private:
@@ -163,6 +169,8 @@ public:
        RouteTemplateManager () : TemplateManager () {}
        ~RouteTemplateManager () {}
 
+       void init ();
+
        void get_templates (vector<TemplateInfo>& templates) const;
 
 private:
@@ -189,7 +197,7 @@ TemplateDialog::TemplateDialog ()
        nb->append_page (*route_tm, _("Track Templates"));
 
        get_vbox()->pack_start (*nb);
-       add_button (_("Ok"), Gtk::RESPONSE_OK);
+       add_button (_("Done"), Gtk::RESPONSE_OK);
 
        get_vbox()->show_all();
 
@@ -201,8 +209,8 @@ TemplateDialog::TemplateDialog ()
 
        signal_hide().connect (sigc::mem_fun (session_tm, &TemplateManager::handle_dirty_description));
        signal_hide().connect (sigc::mem_fun (route_tm, &TemplateManager::handle_dirty_description));
-       nb->signal_switch_page().connect (boost::bind (&TemplateManager::handle_dirty_description, session_tm));
-       nb->signal_switch_page().connect (boost::bind (&TemplateManager::handle_dirty_description, route_tm));
+       nb->signal_switch_page().connect (sigc::hide (sigc::hide (sigc::mem_fun (session_tm, &TemplateManager::handle_dirty_description))));
+       nb->signal_switch_page().connect (sigc::hide (sigc::hide (sigc::mem_fun (route_tm, &TemplateManager::handle_dirty_description))));
 }
 
 TemplateManager::TemplateManager ()
@@ -225,7 +233,7 @@ TemplateManager::TemplateManager ()
 
        _validated_column.set_cell_data_func (_validating_cellrenderer, sigc::mem_fun (*this, &TemplateManager::render_template_names));
        _validating_cellrenderer.signal_edited().connect (sigc::mem_fun (*this, &TemplateManager::validate_edit));
-       _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
+       _cursor_changed_connection = _template_treeview.signal_cursor_changed().connect (sigc::mem_fun (*this, &TemplateManager::row_selection_changed));
        _template_treeview.signal_key_press_event().connect (sigc::mem_fun (*this, &TemplateManager::key_event));
 
        ScrolledWindow* sw = manage (new ScrolledWindow);
@@ -282,18 +290,6 @@ TemplateManager::TemplateManager ()
        _progress_bar.hide ();
 }
 
-void
-TemplateManager::init ()
-{
-       vector<TemplateInfo> templates;
-       get_templates (templates);
-       setup_model (templates);
-
-       _progress_bar.hide ();
-       _description_editor.set_sensitive (false);
-       _save_desc.set_sensitive (false);
-}
-
 void
 TemplateManager::setup_model (const vector<TemplateInfo>& templates)
 {
@@ -317,7 +313,7 @@ TemplateManager::handle_dirty_description ()
        if (_desc_dirty && _current_selection) {
                ArdourDialog dlg (_("Description not saved"), true);
                const string name = _current_selection->get_value (_template_columns.name);
-               Label msg (string_compose (_("The discription of template \"%1\" has been modfied but has not been saved yet.\n"
+               Label msg (string_compose (_("The description of template \"%1\" has been modified but has not been saved yet.\n"
                                             "Do you want to save it?"), name));
                dlg.get_vbox()->pack_start (msg);
                msg.show ();
@@ -332,8 +328,6 @@ TemplateManager::handle_dirty_description ()
                } else {
                        _description_editor.get_buffer()->set_text (_current_selection->get_value (_template_columns.description));
                }
-
-               _desc_dirty = false;
        }
 }
 
@@ -344,17 +338,17 @@ TemplateManager::row_selection_changed ()
                handle_dirty_description ();
        } else {
                _description_editor.get_buffer()->set_text ("");
-               _desc_dirty = false;
        }
 
        _current_selection = _template_treeview.get_selection()->get_selected ();
        if (_current_selection) {
                const string desc = _current_selection->get_value (_template_columns.description);
                _description_editor.get_buffer()->set_text (desc);
-               _desc_dirty = false;
-               _save_desc.set_sensitive (false);
        }
 
+       _desc_dirty = false;
+       _save_desc.set_sensitive (false);
+
        _description_editor.set_sensitive (_current_selection);
        _rename_button.set_sensitive (_current_selection);
        _remove_button.set_sensitive (_current_selection);
@@ -403,7 +397,9 @@ TemplateManager::start_edit ()
        TreeModel::Path path;
        TreeViewColumn* col;
        _template_treeview.get_cursor (path, col);
+       _cursor_changed_connection.block ();
        _template_treeview.set_cursor (path, *col, /*set_editing =*/ true);
+       _cursor_changed_connection.unblock ();
 }
 
 void
@@ -418,7 +414,12 @@ TemplateManager::save_template_desc ()
 {
        const string file_path = template_file (_current_selection);
 
-       const string desc_txt = _description_editor.get_buffer()->get_text ();
+       string desc_txt = _description_editor.get_buffer()->get_text ();
+       string::reverse_iterator wss = desc_txt.rbegin();
+       while (wss != desc_txt.rend() && isspace (*wss)) {
+               desc_txt.erase (--(wss++).base());
+       }
+
        _current_selection->set_value (_template_columns.description, desc_txt);
 
        XMLTree tree;
@@ -428,13 +429,14 @@ TemplateManager::save_template_desc ()
                return;
        }
 
-       tree.root()->remove_nodes (X_("description"));
-       XMLNode* desc = new XMLNode (X_("description"));
-
-       XMLNode* dn = new XMLNode (X_("content"), desc_txt);
-       desc->add_child_nocopy (*dn);
+       tree.root()->remove_nodes_and_delete (X_("description"));
 
-       tree.root()->add_child_nocopy (*desc);
+       if (!desc_txt.empty ()) {
+               XMLNode* desc = new XMLNode (X_("description"));
+               XMLNode* dn = new XMLNode (X_("content"), desc_txt);
+               desc->add_child_nocopy (*dn);
+               tree.root()->add_child_nocopy (*desc);
+       }
 
        if (!tree.write ()) {
                error << string_compose(X_("Could not write to template file \"%1\"."), file_path) << endmsg;
@@ -460,13 +462,14 @@ TemplateManager::key_event (GdkEventKey* ev)
        return false;
 }
 
-static
-bool accept_all_files (string const &, void *)
+static bool
+accept_all_files (string const &, void *)
 {
        return true;
 }
 
-static void _set_progress (Progress* p, size_t n, size_t t)
+static void
+_set_progress (Progress* p, size_t n, size_t t)
 {
        p->set_progress (float (n) / float(t));
 }
@@ -487,13 +490,13 @@ TemplateManager::export_all_templates ()
        g_clear_error (&err);
 
        FileChooserDialog dialog(_("Save Exported Template Archive"), FILE_CHOOSER_ACTION_SAVE);
-       dialog.set_filename (X_("templates.tar.xz"));
+       dialog.set_filename (X_("templates"));
 
        dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
        dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
 
        FileFilter archive_filter;
-       archive_filter.add_pattern (X_("*.tar.xz"));
+       archive_filter.add_pattern (string_compose(X_("*%1"), ARDOUR::template_archive_suffix));
        archive_filter.set_name (_("Template archives"));
        dialog.add_filter (archive_filter);
 
@@ -505,9 +508,7 @@ TemplateManager::export_all_templates ()
        }
 
        string filename = dialog.get_filename ();
-       if (filename.compare (filename.size () - 7, 7, ".tar.xz")) {
-               filename += ".tar.xz";
-       }
+       filename += ARDOUR::template_archive_suffix;
 
        if (g_file_test (filename.c_str(), G_FILE_TEST_EXISTS)) {
                ArdourDialog dlg (_("File exists"), true);
@@ -571,7 +572,7 @@ TemplateManager::import_template_set ()
        dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
 
        FileFilter archive_filter;
-       archive_filter.add_pattern (X_("*.tar.xz"));
+       archive_filter.add_pattern (string_compose(X_("*%1"), ARDOUR::template_archive_suffix));
        archive_filter.set_name (_("Template archives"));
        dialog.add_filter (archive_filter);
 
@@ -661,13 +662,38 @@ TemplateManager::update_progress_gui (float p)
        _progress_bar.set_fraction (p);
 }
 
+void
+SessionTemplateManager::init ()
+{
+       vector<TemplateInfo> templates;
+       get_templates (templates);
+       setup_model (templates);
+
+       _progress_bar.hide ();
+       _description_editor.set_sensitive (false);
+       _save_desc.set_sensitive (false);
+}
+
+void
+RouteTemplateManager::init ()
+{
+       vector<TemplateInfo> templates;
+       get_templates (templates);
+       setup_model (templates);
+
+       _progress_bar.hide ();
+       _description_editor.set_sensitive (false);
+       _save_desc.set_sensitive (false);
+}
+
 void
 SessionTemplateManager::get_templates (vector<TemplateInfo>& templates) const
 {
        find_session_templates (templates, /* read_xml = */ true);
 }
 
-void RouteTemplateManager::get_templates (vector<TemplateInfo>& templates) const
+void
+RouteTemplateManager::get_templates (vector<TemplateInfo>& templates) const
 {
        find_route_templates (templates);
 }
@@ -681,6 +707,10 @@ SessionTemplateManager::rename_template (TreeModel::iterator& item, const Glib::
        const string old_name = item->get_value (_template_columns.name);
        const string new_name = string (new_name_);
 
+       if (old_name == new_name) {
+               return;
+       }
+
        const string old_file_old_path = Glib::build_filename (old_path, old_name+".template");
 
        XMLTree tree;
@@ -785,6 +815,10 @@ RouteTemplateManager::rename_template (TreeModel::iterator& item, const Glib::us
        const string old_filepath = item->get_value (_template_columns.path);
        const string new_filepath = Glib::build_filename (user_route_template_directory(), new_name+".template");
 
+       if (old_name == new_name) {
+               return;
+       }
+
        XMLTree tree;
        if (!tree.read (old_filepath)) {
                error << string_compose (_("Could not parse template file \"%1\"."), old_filepath) << endmsg;