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