re-work VST paths configuration.
[ardour.git] / libs / gtkmm2ext / paths_dialog.cc
1 /*
2     Copyright (C) 2014 Robin Gareus <robin@gareus.org>
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 #include <cstdio>
20
21 #include "i18n.h"
22 #include "pbd/pathexpand.h"
23 #include "gtkmm2ext/paths_dialog.h"
24
25 using namespace Gtk;
26 using namespace std;
27 using namespace Gtkmm2ext;
28
29 PathsDialog::PathsDialog (std::string title, std::string current_paths, std::string default_paths)
30         : Dialog (title, true)
31         , paths_list_view(1, false, Gtk::SELECTION_SINGLE)
32         , add_path_button(_("Add"))
33         , remove_path_button(_("Delete"))
34         , set_default_button(_("Reset to Default"))
35         , _default_paths(default_paths)
36 {
37         set_name ("PathsDialog");
38         set_skip_taskbar_hint (true);
39         set_resizable (true);
40         set_size_request (400, -1);
41
42         paths_list_view.set_border_width (4);
43
44         add_path_button.signal_clicked().connect (sigc::mem_fun (*this, &PathsDialog::add_path));
45         remove_path_button.signal_clicked().connect (sigc::mem_fun (*this, &PathsDialog::remove_path));
46         set_default_button.signal_clicked().connect (sigc::mem_fun (*this, &PathsDialog::set_default));
47         remove_path_button.set_sensitive(false);
48
49         paths_list_view.set_column_title(0,"Path");
50
51         std::vector <std::string> a = PBD::parse_path(current_paths);
52         for(vector<std::string>::const_iterator i = a.begin(); i != a.end(); ++i) {
53                 paths_list_view.append(*i);
54         }
55
56         paths_list_view.get_selection()->signal_changed().connect (mem_fun (*this, &PathsDialog::selection_changed));
57
58         VBox *vbox = manage (new VBox);
59         vbox->pack_start (add_path_button, false, false);
60         vbox->pack_start (remove_path_button, false, false);
61         vbox->pack_start (set_default_button, false, false);
62
63         /* Overall layout */
64         HBox *hbox = manage (new HBox);
65         hbox->pack_start (*vbox, false, false);
66         hbox->pack_start (paths_list_view, true, true); // TODO, wrap in scroll-area ?!
67         hbox->set_spacing (4);
68
69         get_vbox()->set_spacing (4);
70         get_vbox()->pack_start (*hbox, true, true);
71
72         add_button (Stock::CANCEL, RESPONSE_CANCEL);
73         add_button (Stock::OK, RESPONSE_ACCEPT);
74
75         show_all_children ();
76 }
77
78 PathsDialog::~PathsDialog ()
79 {
80 }
81
82 void
83 PathsDialog::on_show() {
84         Dialog::on_show ();
85 }
86
87 std::string
88 PathsDialog::get_serialized_paths() {
89         std::string path;
90         for (unsigned int i = 0; i < paths_list_view.size(); ++i) {
91                 if (i > 0) path += G_SEARCHPATH_SEPARATOR;
92                 path += paths_list_view.get_text(i, 0);
93         }
94         return path;
95 }
96
97 void
98 PathsDialog::selection_changed () {
99         std::vector<int> selection = paths_list_view.get_selected();
100         if (selection.size() > 0) {
101                 remove_path_button.set_sensitive(true);
102         } else {
103                 remove_path_button.set_sensitive(false);
104         }
105 }
106
107 void
108 PathsDialog::add_path() {
109         Gtk::FileChooserDialog d (_("Add folder to search path"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
110         d.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
111         d.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
112         ResponseType r = (ResponseType) d.run ();
113         if (r == Gtk::RESPONSE_OK) {
114                 std::string dir = d.get_filename();
115                 if (Glib::file_test (dir, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
116                         bool dup = false;
117                         for (unsigned int i = 0; i < paths_list_view.size(); ++i) {
118                                 if (paths_list_view.get_text(i, 0) == dir) {
119                                         dup = true;
120                                         break;
121                                 }
122                         }
123                         if (!dup) {
124                                 paths_list_view.prepend(dir);
125                         }
126                 }
127         }
128 }
129
130 void
131 PathsDialog::remove_path() {
132         std::vector<int> selection = paths_list_view.get_selected();
133         if (selection.size() == 0 ) { return ; }
134
135         /* Gtk::ListViewText internals to delete row(s) */
136         Gtk::TreeModel::iterator row_it = paths_list_view.get_selection()->get_selected();
137         Glib::RefPtr<Gtk::TreeModel> reftm = paths_list_view.get_model();
138         Glib::RefPtr<Gtk::TreeStore> refStore = Glib::RefPtr<Gtk::TreeStore>::cast_dynamic(reftm);
139         if(refStore) {
140                 refStore->erase(row_it);
141                 return;
142         }
143         Glib::RefPtr<Gtk::ListStore> refLStore = Glib::RefPtr<Gtk::ListStore>::cast_dynamic(reftm);
144         if(refLStore){
145                 refLStore->erase(row_it);
146                 return;
147         }
148 }
149
150 void
151 PathsDialog::set_default() {
152
153         paths_list_view.clear_items();
154         std::vector <std::string> a = PBD::parse_path(_default_paths);
155         for(vector<std::string>::const_iterator i = a.begin(); i != a.end(); ++i) {
156                 paths_list_view.append(*i);
157         }
158 }