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