enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 "pbd/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 (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)
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_text(*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
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));
114         }
115
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)) {
122                         bool dup = false;
123                         for (unsigned int i = 0; i < paths_list_view.size(); ++i) {
124                                 if (paths_list_view.get_text(i, 0) == dir) {
125                                         dup = true;
126                                         break;
127                                 }
128                         }
129                         if (!dup) {
130                                 paths_list_view.prepend_text(dir);
131                         }
132                 }
133         }
134 }
135
136 void
137 PathsDialog::remove_path() {
138         std::vector<int> selection = paths_list_view.get_selected();
139         if (selection.size() == 0 ) { return ; }
140
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);
145         if(refStore) {
146                 refStore->erase(row_it);
147                 return;
148         }
149         Glib::RefPtr<Gtk::ListStore> refLStore = Glib::RefPtr<Gtk::ListStore>::cast_dynamic(reftm);
150         if(refLStore){
151                 refLStore->erase(row_it);
152                 return;
153         }
154 }
155
156 void
157 PathsDialog::set_default() {
158
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);
163         }
164 }