(Source List) Fix drag-n-drop.
[ardour.git] / gtk2_ardour / search_path_option.cc
1 /*
2     Copyright (C) 2010 Paul Davis
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 <gtkmm/stock.h>
20
21 #include "pbd/strsplit.h"
22 #include "pbd/compose.h"
23 #include "pbd/shortpath.h"
24
25 #include "gtkmm2ext/utils.h"
26
27 #include "search_path_option.h"
28 #include "pbd/i18n.h"
29
30 using namespace std;
31 using namespace Gtk;
32
33 SearchPathOption::SearchPathOption (const string& pathname, const string& label,
34                                     const string& default_path,
35                                     sigc::slot<std::string> get, sigc::slot<bool, std::string> set)
36         : Option (pathname, label)
37         , _get (get)
38         , _set (set)
39         , add_chooser (_("Select folder to search for media"), FILE_CHOOSER_ACTION_SELECT_FOLDER)
40 {
41         Gtkmm2ext::add_volume_shortcuts (add_chooser);
42         add_chooser.signal_file_set().connect (sigc::mem_fun (*this, &SearchPathOption::path_chosen));
43
44         HBox* hbox = manage (new HBox);
45
46         hbox->set_border_width (12);
47         hbox->set_spacing (6);
48         hbox->pack_end (add_chooser, true, true);
49         hbox->pack_end (*manage (new Label (_("Click to add a new location"))), false, false);
50         hbox->show_all ();
51
52         vbox.pack_start (path_box);
53         vbox.pack_end (*hbox);
54
55         session_label.set_use_markup (true);
56         session_label.set_markup (string_compose ("<i>%1 (%2)</i>", _("the session folder"), short_path (default_path, 32)));
57         session_label.set_alignment (0.0, 0.5);
58         session_label.show ();
59
60         path_box.pack_start (session_label);
61 }
62
63 SearchPathOption::~SearchPathOption()
64 {
65
66
67 }
68
69 void
70 SearchPathOption::path_chosen ()
71 {
72         string path = add_chooser.get_filename ();
73         add_path (path);
74         changed ();
75 }
76
77 void
78 SearchPathOption::add_to_page (OptionEditorPage* p)
79 {
80         int const n = p->table.property_n_rows();
81         p->table.resize (n + 1, 3);
82
83         Label* label = manage (new Label);
84         label->set_alignment (0.0, 0.0);
85         label->set_text (string_compose ("%1", _name));
86
87         p->table.attach (*label, 1, 2, n, n + 1, FILL | EXPAND);
88         p->table.attach (vbox, 2, 3, n, n + 1, FILL | EXPAND);
89 }
90
91 void
92 SearchPathOption::clear ()
93 {
94         path_box.remove (session_label);
95         for (list<PathEntry*>::iterator p = paths.begin(); p != paths.end(); ++p) {
96                 path_box.remove ((*p)->box);
97                 delete *p;
98         }
99         paths.clear ();
100 }
101
102 void
103 SearchPathOption::set_state_from_config ()
104 {
105         string str = _get ();
106         vector<string> dirs;
107
108         clear ();
109         path_box.pack_start (session_label);
110
111         split (str, dirs, G_SEARCHPATH_SEPARATOR);
112
113         for (vector<string>::iterator d = dirs.begin(); d != dirs.end(); ++d) {
114                 add_path (*d);
115         }
116 }
117
118 void
119 SearchPathOption::changed ()
120 {
121         string str;
122
123         for (list<PathEntry*>::iterator p = paths.begin(); p != paths.end(); ++p) {
124
125                 if (!str.empty()) {
126                         str += G_SEARCHPATH_SEPARATOR;
127                 }
128                 str += (*p)->entry.get_text ();
129         }
130
131         _set (str);
132 }
133
134 void
135 SearchPathOption::add_path (const string& path, bool removable)
136 {
137         PathEntry* pe = new PathEntry (path, removable);
138         paths.push_back (pe);
139         path_box.pack_start (pe->box, false, false);
140         pe->remove_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &SearchPathOption::remove_path), pe));
141 }
142
143 void
144 SearchPathOption::remove_path (PathEntry* pe)
145 {
146         path_box.remove (pe->box);
147         paths.remove (pe);
148         delete pe;
149         changed ();
150 }
151
152 SearchPathOption::PathEntry::PathEntry (const std::string& path, bool removable)
153         : remove_button (Stock::REMOVE)
154 {
155         entry.set_text (path);
156         entry.show ();
157
158         box.set_spacing (6);
159         box.set_homogeneous (false);
160         box.pack_start (entry, true, true);
161
162         if (removable) {
163                 box.pack_start (remove_button, false, false);
164                 remove_button.show ();
165         }
166
167         box.show ();
168 }