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 (std::string title, std::string user_paths, std::string fixed_paths)
30 : Dialog (title, true)
31 , paths_list_view(2, false, Gtk::SELECTION_SINGLE)
32 , add_path_button(_("Add"))
33 , remove_path_button(_("Delete"))
35 set_name ("PathsDialog");
36 set_skip_taskbar_hint (true);
38 set_size_request (400, -1);
40 paths_list_view.set_border_width (4);
42 add_path_button.signal_clicked().connect (sigc::mem_fun (*this, &PathsDialog::add_path));
43 remove_path_button.signal_clicked().connect (sigc::mem_fun (*this, &PathsDialog::remove_path));
44 remove_path_button.set_sensitive(false);
46 paths_list_view.set_column_title(0,"Type");
47 paths_list_view.set_column_title(1,"Path");
49 /* TODO fill in Text View */
50 std::vector <std::string> a = PBD::parse_path(user_paths);
51 for(vector<std::string>::const_iterator i = a.begin(); i != a.end(); ++i) {
52 int row = paths_list_view.append(_("user"));
53 paths_list_view.set_text(row, 1, *i);
55 a = PBD::parse_path(fixed_paths);
56 for(vector<std::string>::const_iterator i = a.begin(); i != a.end(); ++i) {
57 int row = paths_list_view.append( _("sys"));
58 paths_list_view.set_text(row, 1, *i);
61 paths_list_view.get_selection()->signal_changed().connect (mem_fun (*this, &PathsDialog::selection_changed));
64 HBox *hbox = manage (new HBox);
65 hbox->pack_start (paths_list_view, true, true);
66 get_vbox()->set_spacing (4);
67 get_vbox()->pack_start (*hbox, true, true);
69 add_button (Stock::CANCEL, RESPONSE_CANCEL);
70 add_button (Stock::OK, RESPONSE_ACCEPT);
71 get_action_area()->pack_start (add_path_button, false, false);
72 get_action_area()->pack_start (remove_path_button, false, false);
77 PathsDialog::~PathsDialog ()
82 PathsDialog::on_show() {
87 PathsDialog::get_serialized_paths(bool include_fixed) {
89 for (unsigned int i = 0; i < paths_list_view.size(); ++i) {
90 if (!include_fixed && paths_list_view.get_text(i, 0) != _("user")) continue;
91 if (i > 0) path += G_SEARCHPATH_SEPARATOR;
92 path += paths_list_view.get_text(i, 1);
98 PathsDialog::selection_changed () {
99 std::vector<int> selection = paths_list_view.get_selected();
100 if (selection.size() > 0) {
101 const int row = selection.at(0);
102 if (paths_list_view.get_text(row, 0) == _("user")) {
103 remove_path_button.set_sensitive(true);
107 remove_path_button.set_sensitive(false);
111 PathsDialog::add_path() {
112 Gtk::FileChooserDialog d (_("Add folder to search path"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
113 d.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
114 d.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
115 ResponseType r = (ResponseType) d.run ();
116 if (r == Gtk::RESPONSE_OK) {
117 std::string dir = d.get_filename();
118 if (Glib::file_test (dir, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
119 paths_list_view.prepend(_("user"));
120 paths_list_view.set_text(0, 1, dir);
126 PathsDialog::remove_path() {
127 std::vector<int> selection = paths_list_view.get_selected();
128 if (selection.size() != 1) { return ; }
129 const int row = selection.at(0);
130 if (paths_list_view.get_text(row, 0) != _("user")) { return ; }
132 /* Gtk::ListViewText internals to delete row(s) */
133 Gtk::TreeModel::iterator row_it = paths_list_view.get_selection()->get_selected();
134 Glib::RefPtr<Gtk::TreeModel> reftm = paths_list_view.get_model();
135 Glib::RefPtr<Gtk::TreeStore> refStore = Glib::RefPtr<Gtk::TreeStore>::cast_dynamic(reftm);
137 refStore->erase(row_it);
140 Glib::RefPtr<Gtk::ListStore> refLStore = Glib::RefPtr<Gtk::ListStore>::cast_dynamic(reftm);
142 refLStore->erase(row_it);