2 Copyright (C) 2014 Robin Gareus <robin@gareus.org>
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.
22 #include "pbd/pathexpand.h"
23 #include "gtkmm2ext/paths_dialog.h"
27 using namespace Gtkmm2ext;
29 PathsDialog::PathsDialog (Gtk::Window& parent, std::string title, std::string current_paths, std::string default_paths)
30 : Dialog (title, parent, 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)
37 set_name ("PathsDialog");
38 set_skip_taskbar_hint (true);
40 set_size_request (400, -1);
42 paths_list_view.set_border_width (4);
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);
49 paths_list_view.set_column_title(0,"Path");
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_text(*i);
56 paths_list_view.get_selection()->signal_changed().connect (mem_fun (*this, &PathsDialog::selection_changed));
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);
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);
69 get_vbox()->set_spacing (4);
70 get_vbox()->pack_start (*hbox, true, true);
72 add_button (Stock::CANCEL, RESPONSE_CANCEL);
73 add_button (Stock::OK, RESPONSE_ACCEPT);
78 PathsDialog::~PathsDialog ()
83 PathsDialog::on_show() {
88 PathsDialog::get_serialized_paths() {
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);
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);
103 remove_path_button.set_sensitive(false);
108 PathsDialog::add_path() {
109 Gtk::FileChooserDialog d (_("Add folder to search path"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
111 std::vector<int> selection = paths_list_view.get_selected();
112 if (selection.size() == 1 ) {
113 d.set_current_folder(paths_list_view.get_text(selection.at(0), 0));
116 d.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
117 d.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
118 ResponseType r = (ResponseType) d.run ();
119 if (r == Gtk::RESPONSE_OK) {
120 std::string dir = d.get_filename();
121 if (Glib::file_test (dir, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
123 for (unsigned int i = 0; i < paths_list_view.size(); ++i) {
124 if (paths_list_view.get_text(i, 0) == dir) {
130 paths_list_view.prepend_text(dir);
137 PathsDialog::remove_path() {
138 std::vector<int> selection = paths_list_view.get_selected();
139 if (selection.size() == 0 ) { return ; }
141 /* Gtk::ListViewText internals to delete row(s) */
142 Gtk::TreeModel::iterator row_it = paths_list_view.get_selection()->get_selected();
143 Glib::RefPtr<Gtk::TreeModel> reftm = paths_list_view.get_model();
144 Glib::RefPtr<Gtk::TreeStore> refStore = Glib::RefPtr<Gtk::TreeStore>::cast_dynamic(reftm);
146 refStore->erase(row_it);
149 Glib::RefPtr<Gtk::ListStore> refLStore = Glib::RefPtr<Gtk::ListStore>::cast_dynamic(reftm);
151 refLStore->erase(row_it);
157 PathsDialog::set_default() {
159 paths_list_view.clear_items();
160 std::vector <std::string> a = PBD::parse_path(_default_paths);
161 for(vector<std::string>::const_iterator i = a.begin(); i != a.end(); ++i) {
162 paths_list_view.append_text(*i);