7b42a85f32a7a1322c9dfcafa6e7f2c24c318a80
[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 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"))
34 {
35         set_name ("PathsDialog");
36         set_skip_taskbar_hint (true);
37         set_resizable (true);
38         set_size_request (400, -1);
39
40         paths_list_view.set_border_width (4);
41
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);
45
46         paths_list_view.set_column_title(0,"Type");
47         paths_list_view.set_column_title(1,"Path");
48
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);
54         }
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);
59         }
60
61         paths_list_view.get_selection()->signal_changed().connect (mem_fun (*this, &PathsDialog::selection_changed));
62
63         /* Overall layout */
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);
68
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);
73
74         show_all_children ();
75 }
76
77 PathsDialog::~PathsDialog ()
78 {
79 }
80
81 void
82 PathsDialog::on_show() {
83         Dialog::on_show ();
84 }
85
86 std::string
87 PathsDialog::get_serialized_paths(bool include_fixed) {
88         std::string path;
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);
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                 const int row = selection.at(0);
102                 if (paths_list_view.get_text(row, 0) == _("user")) {
103                         remove_path_button.set_sensitive(true);
104                         return;
105                 }
106         }
107         remove_path_button.set_sensitive(false);
108 }
109
110 void
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);
121                 }
122         }
123 }
124
125 void
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 ; }
131
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);
136         if(refStore) {
137                 refStore->erase(row_it);
138                 return;
139         }
140         Glib::RefPtr<Gtk::ListStore> refLStore = Glib::RefPtr<Gtk::ListStore>::cast_dynamic(reftm);
141         if(refLStore){
142                 refLStore->erase(row_it);
143                 return;
144         }
145 }